#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
struct muchie {
int vecin;
int deplasare;
};
vector<muchie> g[30001];
int dist[30001];
bool viz[30001];
int main() {
int n, m, x, y;
fin>> n >> m >> x >> y;
for (int c = 1; c<= m; c++ ) {
int i, j, d;
fin >> i >> j >> d;
g[i].push_back({j, d});
g[j].push_back({i, -d});
}
queue<int> q;
dist[x] = 0; //pornim din nodul x
viz[x] = true;
q.push(x); // nodul de start
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == y) {
break;
}
for (int vecin = 0; vecin < g[u].size(); vecin++) {
int v = g[u][vecin].vecin;
int d = g[u][vecin].deplasare;
if (!viz[v]) {
viz[v] = true;
dist[v] = dist[u] + d;
q.push(v);
}
}
}
fout << dist[y] << "\n";
return 0;
}