#include <fstream>
#include <vector>
#include <utility>
#include <queue>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
std::vector<std::pair<int, int>> v[30001];
int dist[30001];
bool viz[30001];
int main() {
int n, m, x, y;
fin >> n >> m >> x >> y;
int a, b, c;
for (int i = 1; i <= m; i++) {
fin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, -c});
}
std::queue<int> q;
dist[x] = 0;
viz[x] = true;
q.push(x);
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto per : v[nod]) {
int vecin = per.first;
int cost = per.second;
if (!viz[vecin]) {
dist[vecin] = dist[nod] + cost;
viz[vecin] = true;
q.push(vecin);
}
if (vecin == y) {
fout << abs(dist[y]);
return 0;
}
}
}
return 0;
}