#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
long long poz[30005];
bool vizitat[30005];
vector<pair<int, int>> adj[30005];
int main() {
fin >> n >> m >> x >> y;
for (int i = 1; i <= m; i++) {
int a, b, d;
fin >> a >> b >> d;
adj[a].push_back({b, d});
adj[b].push_back({a, -d});
}
queue<int> q;
q.push(x);
vizitat[x] = true;
poz[x] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto e : adj[u]) {
int w = e.first;
if (!vizitat[w]) {
vizitat[w] = true;
poz[w] = poz[u] + e.second;
q.push(w);
}
}
}
fout << llabs(poz[y]) << '\n';
return 0;
}