#include <stdio.h>
#include <stdlib.h>
#define FIN "sate.in"
#define FOUT "sate.out"
#define MAXN 30001
#define MAXM 100024
int n, m, x, y;
struct elem { int x, c; elem *r; } * L[MAXN];
void push(int a, int b, int c)
{
elem *temp = new elem;
temp->x = b; temp->c = c; temp->r = L[a];
L[a] = temp;
}
void citire()
{
int i, a, b, c;
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
scanf("%d%d%d%d", &n, &m, &x, &y);
for (i=0; i<m; ++i) {
scanf("%d %d %d", &a, &b, &c);
push(a, b, c);
push(b, a, -c);
}
}
long long dist = 0;
struct ceva { int s; elem **tembel, *p, *last; } S[MAXM*5]; // well teoretic e *2 dar ..
int t = -1;
void fixme(int s)
{
S[++t] = (ceva) { x, 0, L[s], 0 };
while (S[t].s != y && t >= 0) {
if (S[t].p) {
if (t && S[t].p->x == S[t-1].s) {
S[t].last = S[t].p, S[t].p = S[t].p->r;
continue;
}
// eclipsam...
S[t].tembel = S[t].last ? &S[t].last->r : &L[S[t].s];
*S[t].tembel = S[t].p->r;
//fprintf(stderr, "%d => %d (%d)\n", S[t].s, S[t].p->x, S[t].p->c);
dist += S[t].p->c;
// merg mai departe...
S[t+1] = (ceva) { S[t].p->x, 0, L[ S[t].p->x ], 0 }; ++t;
continue;
}
// ne intoarcem ...
--t;
dist -= S[t].p->c;
*S[t].tembel = S[t].p;
S[t].last = S[t].p, S[t].p = S[t].p->r;
}
printf("%lld\n", dist);
}
int main()
{
citire();
fixme(x);
return 0;
}