#include<fstream>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int N,M,x,y,s[30003],a[100030],b[100030],d[100030],i;
int main()
{
    for(f>>N>>M>>x>>y,s[x]=i=1;i<=M;++i)
        f>>a[i]>>b[i]>>d[i];
    while(!s[y])
        for(i=1;i<=M;++i)
            if(s[a[i]]&&!s[b[i]])
                s[b[i]]=s[a[i]]+d[i];
            else if(!s[a[i]]&&s[b[i]])
                s[a[i]]=s[b[i]]-d[i];
    g<<s[y]-1;
    return 0;
}