#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
vector<pair<int, int>> g[30005];
int d[30005];
bool vz[30005];
void bfs(int s) {
queue<int> q;
vz[s] = 1;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == y)
return;
for (auto &e : g[u]) {
int p = e.first;
int w = e.second;
if (!vz[p]) {
vz[p] = 1;
d[p] = d[u] + w;
q.push(p);
}
}
}
}
int main() {
fin >> n >> m >> x >> y;
int a, b, c;
for (int i = 1; i <= m; i++) {
fin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, -c});
}
bfs(x);
fout << abs(d[y]) << "\n";
return 0;
}