// O(n+m)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
const int NMAX = 30005;
struct Nod
{
int vecin;
int distanta;
};
vector<Nod> adj[NMAX];
int distanta[NMAX];
bool viz[NMAX];
int main()
{
ifstream fin("sate.in");
ofstream fout("sate.out");
if (!fin || !fout) return 1;
int n, m, X, Y;
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);
viz[X] = true;
distanta[X] = 0;
// BFS
while (!q.empty())
{
int nod = q.front();
q.pop();
for (Nod x : adj[nod])
{
int vecin = x.vecin;
int d = x.distanta;
if (!viz[vecin])
{
viz[vecin] = true;
if (vecin > nod)
distanta[vecin] = distanta[nod] + d;
else
distanta[vecin] = distanta[nod] - d;
q.push(vecin);
}
}
}
fout << abs(distanta[Y]);
return 0;
}