#include<fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,X,Y;
queue<pair<int,int>> q;
bitset<30005> viz;
vector<pair<int,int>> L[30005];
int main() {
int a,b,d;
fin>>n>>m>>X>>Y;
for (int i=1;i<=m;i++) {
fin>>a>>b>>d;
L[a].push_back(make_pair(b,d));
L[b].push_back(make_pair(a,-d));
}
q.push(make_pair(X,0));
viz[X]=1;
while (!q.empty()) {
auto[k,d]=q.front();
q.pop();
for (auto& w:L[k]) {
if (viz[w.first]==0) {
q.push(make_pair(w.first,d+w.second));
viz[w.first] = 1;
if (w.first == Y) {
fout<<d+w.second<<"\n";
return 0;
}
}
}
}
}