#include <cstdio>
#include <vector>
using namespace std;
#define INF 666000666
#define NMAX 30030
#define pb push_back
#define LINESIZE 30
vector<int> C[NMAX];
vector<unsigned short> A[NMAX];
unsigned short N, X, Y, Q[NMAX], G[NMAX], F[NMAX];
int M, hi, Dist[NMAX];
FILE *f;
#define read3ints(l, x, y, z) \
{ \
int c; \
for (c = 0; l[c] < '0' || l[c] > '9'; c++); \
*x = 0; \
for (;l[c] >= '0' && l[c] <= '9'; c++) \
*x = (*x * 10) + (l[c] - '0'); \
for (;l[c] < '0' || l[c] > '9';c++); \
*y = 0; \
for (;l[c] >= '0' && l[c] <= '9';c++)\
*y = (*y * 10) + (l[c] - '0');\
for (;l[c] < '0' || l[c] > '9';c++); \
*z = 0; \
for (;l[c] >= '0' && l[c] <= '9';c++)\
*z = (*z * 10) + (l[c] - '0');\
}
#define read4ints(l, x, y, z, t)\
{ \
int c;\
for (c = 0; l[c] < '0' || l[c] > '9'; c++); \
*x = 0;\
for (;l[c] >= '0' && l[c] <= '9'; c++) \
*x = (*x * 10) + (l[c] - '0'); \
for (;l[c] < '0' || l[c] > '9';c++); \
*y = 0;\
for (;l[c] >= '0' && l[c] <= '9';c++)\
*y = (*y * 10) + (l[c] - '0');\
for (;l[c] < '0' || l[c] > '9';c++); \
*z = 0;\
for (;l[c] >= '0' && l[c] <= '9';c++)\
*z = (*z * 10) + (l[c] - '0');\
for (;l[c] < '0' || l[c] > '9';c++); \
*t = 0;\
for (;l[c] >= '0' && l[c] <= '9';c++)\
*t = (*t * 10) + (l[c] - '0');\
}
void read()
{
unsigned short i, j, k;
int d;
char line[LINESIZE];
f=fopen("sate.in", "r");
// setbuffer(f,buf,siceof(buf));
// fscanf(f,"%d%d%d%d", &N, &M, &X, &Y);
fgets(line, LINESIZE - 1, f);
read4ints(line, &N, &M, &X, &Y);
for (k = 0; k < M; k++)
{
// fscanf(f,"%d%d%d", &i, &j, &d);
fgets(line, LINESIZE - 1, f);
read3ints(line, &i, &j, &d);
A[i].pb(j); A[j].pb(i); C[i].pb(d); C[j].pb(d);
G[i]++; G[j]++;
if (i==X) { Q[hi] = j; hi++; }
if (j==X) { Q[hi] = i; hi++; }
}
fclose(f);
}
void solve()
{
unsigned short i, j, f, n;
int lo, d;
for (i = 0; i < NMAX; i++) Dist[i] = INF;
Dist[X] = 0;
for (n = G[X], i = 0; i < n; i++)
Dist[ A[X][i] ] = C[X][i];
freopen("sate.out", "w", stdout);
if (Dist[Y]<INF) { printf("%d\n", Dist[Y]); exit(0); }
F[X] = 1;
for (lo = 0; lo < hi; lo++)
{
i = Q[lo];
for (n = G[i], j = 0; j < n; j++)
{
f = A[i][j]; d = C[i][j];
if (!F[f])
{
//case 1
if ( (X < i && i < f) || (X > i && i > f) ) Dist[f] = Dist[i]+d;
else
//case 2
if ( (X < f && f < i) || (X > f && f > i) ) Dist[f] = Dist[i]-d;
else
//case 3
if ( (f < X && X < i) || (i < X && X < f) ) Dist[f] = d-Dist[i];
Q[hi] = f; hi++;
F[f] = 1;
if (f==Y)
{ printf("%d\n", Dist[f]); exit(0); }
}
}
}
}
int main()
{
read();
solve();
return 0;
}