#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX = 30005;
int N, M, X, Y, d[MAX], viz[MAX];
vector < pair <int, int> > A[MAX];
queue <int> q;
void solve() {
q.push(X);
viz[X] = 1;
while(!q.empty()) {
int sat = q.front();
q.pop();
for(auto i : A[sat])
if(!viz[i.first]) {
viz[i.first] = 1;
q.push(i.first);
d[i.first] = d[sat] + i.second;
if(i.first == Y) return;
}
}
}
int main() {
fin >> N >> M >> X >> Y;
int a, b, cost;
while(M--) {
fin >> a >> b >> cost;
A[a].push_back({b, cost});
A[b].push_back({a, -cost});
}
solve();
fout << d[Y];
}