#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream f("sate.in");
ofstream g("sate.out");
int n, m, x, y;
f >> n >> m >> x >> y;
vector<vector<pair<int, int>>> a(n + 1);
while (m--) {
int i, j, d;
f >> i >> j >> d;
a[i].push_back({j, d});
a[j].push_back({i, -d});
}
vector<long long> p(n + 1);
vector<bool> v(n + 1);
queue<int> q;
q.push(x);
v[x] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto e : a[u])
if (!v[e.first]) {
v[e.first] = 1;
p[e.first] = p[u] + e.second;
q.push(e.first);
}
}
g << llabs(p[y]);
}