#include <bits/stdc++.h>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int INF = 1e9;
int main() {
int n, m, x, y;
in >> n >> m >> x >> y;
vector<vector<pair<int, int>>> adj(n + 1);
for (int i = 1; i <= m; i++) {
int a, b, c;
in >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, -c});
}
vector<int> dist(n + 1, INF);
queue<int> q;
dist[x] = 0;
q.push(x);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == y) break;
for (auto muchie : adj[u]) {
int v = muchie.first;
int cost = muchie.second;
if (dist[v] == INF) {
dist[v] = dist[u] + cost;
q.push(v);
}
}
}
out << dist[y] << "\n";
return 0;
}