#include<stdio.h>
#include<stdlib.h>
typedef struct O {
int i,c;
struct O *n;
}N;
N *l[30001],*t;
int i,n,m,x,y,j,p,u,k,s[30001],e[30001];
void A(int i,int j,int k)
{
N *p=(N*)malloc(sizeof(N));
p->i=j,p->c=k,p->n=l[i],l[i]=p;
}
int main()
{
freopen("sate.in","r",stdin),freopen("sate.out","w",stdout),scanf("%d%d%d%d",&n,&m,&x,&y);
while(m--)
scanf("%d%d%d",&i,&j,&k),A(i,j,k),A(j,i,-k);
for(s[u++]=x;p<u&&!e[y];++p)
for(j=s[p],t=l[j];t;t=t->n)
if(!e[t->i])
e[s[u++]=t->i]=t->c+e[j];
printf("%d",e[y]);
return 0;
}