// https://www.infoarena.ro/problema/sate
// Complexitate: O(N + M) - BFS din X; fiecare sat e vizitat o data, fiecare muchie de 2 ori
// poz[sat] = pozitia satului fata de X (spre dreapta +d, spre stanga -d)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 30005;
FILE *fin = fopen("sate.in", "r");
char buf[1 << 16];
int bpos = 0, blen = 0;
char urm() { // urmatorul caracter din fisier (citire rapida pe bucati)
if (bpos == blen) {
blen = fread(buf, 1, sizeof(buf), fin);
bpos = 0;
if (blen <= 0) return 0;
}
return buf[bpos++];
}
int citeste() {
char c = urm();
while (c && (c < '0' || c > '9')) c = urm();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = urm();
}
return x;
}
vector<pair<int, int>> g[NMAX]; // (vecin, distanta cu semn)
int poz[NMAX];
bool viz[NMAX];
int main() {
citeste(); // N, nu avem nevoie de el
int m = citeste(), X = citeste(), Y = citeste();
for (int i = 0; i < m; i++) {
int x = citeste(), y = citeste(), d = citeste();
g[x].push_back({y, d}); // x < y: y e la dreapta lui x
g[y].push_back({x, -d});
}
queue<int> q;
q.push(X);
viz[X] = true;
while (!q.empty() && !viz[Y]) {
int nod = q.front();
q.pop();
for (auto [v, d] : g[nod])
if (!viz[v]) {
viz[v] = true;
poz[v] = poz[nod] + d;
q.push(v);
}
}
ofstream fout("sate.out");
fout << abs(poz[Y]) << "\n";
return 0;
}