#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAXN = 30005;
struct Muchie {
int nod;
int cost;
};
vector<Muchie> adj[MAXN];
int poz[MAXN];
bool viz[MAXN];
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
int n, m, x, y;
if (!(fin >> n >> m >> x >> y)) return 0;
for (int k = 0; k < m; ++k) {
int i, j, d;
fin >> i >> j >> d;
// i < j, deci poz[j] = poz[i] + d si poz[i] = poz[j] - d
adj[i].push_back({j, d});
adj[j].push_back({i, -d});
}
queue<int> q;
q.push(x);
viz[x] = true;
poz[x] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == y) {
break;
}
for (const auto& edge : adj[u]) {
int v = edge.nod;
int cost = edge.cost;
if (!viz[v]) {
viz[v] = true;
poz[v] = poz[u] + cost;
q.push(v);
}
}
}
fout << abs(poz[y] - poz[x]) << "\n";
return 0;
}