#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1000;
int n, m, x, y;
vector<int> graf[NMAX + 1];
int d[NMAX + 1], mat[NMAX + 1][NMAX + 1];
void BFS(int nod)
{
for (int i = 1; i <= n; i ++)
{
d[i] = NMAX + 1;
}
d[nod] = 0;
queue<int> q;
q.push(nod);
while (!q.empty())
{
int a = q.front();
if (a == y)
break;
q.pop();
for (auto i : graf[a])
{
d[i] = d[a] + mat[i][a];
q.push(i);
}
}
}
int main()
{
ifstream cin("sate.in");
ofstream cout("sate.out");
cin >> n >> m >> x >> y;
for (int i = 1; i <= m; i ++)
{
int a, b; cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
cin >> mat[a][b];
mat[b][a] = -mat[a][b];
}
BFS(x);
cout << abs(d[y]);
}