#include <fstream>
#include <queue>
#include <vector>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
struct Edge {
int node;
int dist;
};
std::vector<std::vector<Edge>> graph;
std::vector<int> dist;
void bfs(int start) {
std::queue<int> q;
q.push(start);
dist[start] = 1;
while (!q.empty()) {
int curr = q.front();
q.pop();
for (auto &edge : graph[curr]) {
int neigh = edge.node;
int curr_to_neigh_dist = edge.dist;
if (dist[neigh] == 0) {
dist[neigh] = dist[curr] + curr_to_neigh_dist;
q.push(neigh);
}
}
}
}
int main() {
int n, m, x, y;
fin >> n >> m >> x >> y;
graph.resize(n + 1);
dist.resize(n + 1);
for (int i = 0; i < m; i++) {
int a, b, d;
fin >> a >> b >> d;
graph[a].push_back({b, d});
graph[b].push_back({a, -d});
}
bfs(x);
fout << dist[y] - 1;
return 0;
}