#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <math.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector<bool> viz;
queue<int> q;
struct muchie{
int dr,d,urm;
}M[200050];
int cap[30010],ctr=0;
long long poz[30010];
void adauga(int a,int b,int d) {
ctr++;
M[ctr].dr = b;
M[ctr].d = d;
M[ctr].urm = cap[a];
cap[a] = ctr;
}
int main() {
int n,m,x,y;
f>>n>>m>>x>>y;
viz.assign(n+1,false);
for(int i=1;i<=m;i++) {
int st,dr,D;
f>>st>>dr>>D;
adauga(st,dr,D);
adauga(dr,st,-D);
}
poz[x] = 0;
q.push(x);
while(!q.empty()) {
int nod = q.front();
q.pop();
int i = cap[nod];
while(i!=0) {
if(viz[M[i].dr]==false) {
viz[M[i].dr]=true;
poz[M[i].dr]=poz[nod]+M[i].d;
q.push(M[i].dr);
}
i = M[i].urm;
}
}
g<<abs(poz[y]-poz[x]);
return 0;
}