#include <bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
struct graf
{
int nod, cost;
};
int n, m, i, j, x, X, Y, p[30002];
vector <graf> a[30002];
bool v[30002];
void BFS(int nod) {
queue <int> q;
q.push(nod);
p[X] = 0;
v[nod] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (graf it : a[x]) {
if (!v[it.nod]) {
p[it.nod] = p[x] + it.cost;
q.push(it.nod);
v[it.nod] = 1;
}
}
if (x == Y)
g << p[Y], exit(0);
}
}
int main()
{
f >> n >> m >> X >> Y;
while (m--) {
f >> i >> j >> x;
a[i].push_back({j, x});
a[j].push_back({i, -x});
}
BFS(X);
g << p[Y] - p[X];
return 0;
}