#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int main() {
int n, m, x, y;
fin >> n >> m >> x >> y;
// lista de adiacenta: (vecin, cost cu semn)
vector<vector<pair<int, int>>> cost(n + 1);
for (int k = 0; k < m; k++) {
int i, j, d;
fin >> i >> j >> d;
cost[i].push_back({j, d}); // P[j] = P[i] + d
cost[j].push_back({i, -d}); // P[i] = P[j] - d
}
vector<long long> P(n + 1, 0);
vector<bool> vis(n + 1, false);
queue<int> q;
q.push(x);
vis[x] = true;
P[x] = 0;
while (!q.empty() && !vis[y]) {
int u = q.front();
q.pop();
for (auto &e : cost[u]) {
int v = e.first;
int c = e.second;
if (!vis[v]) {
vis[v] = true;
P[v] = P[u] + c;
q.push(v);
}
}
}
long long raspuns = P[y] - P[x];
if (raspuns < 0) raspuns = -raspuns;
fout << raspuns << "\n";
return 0;
}