#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int nmax = 30001;
vector<pair<int, int>> graf[nmax];
int distanta[nmax];
bool vizitat[nmax];
int main(){
int n, m, x, y;
fin >> n >> m >> x >> y;
for (int i = 0; i < m; i++){
int a, b, d;
fin >> a >> b >> d;
graf[a].push_back({b, d});
graf[b].push_back({a, -d});
}
queue<int> q;
q.push(x);
vizitat[x] = true;
distanta[x] = 0;
while (!q.empty()){
int nod = q.front();
q.pop();
for (auto vecin : graf[nod]){
int urmator = vecin.first;
int d = vecin.second;
if (!vizitat[urmator]){
vizitat[urmator] = true;
distanta[urmator] = distanta[nod] + d;
q.push(urmator);
}
}
}
if (distanta[y] < 0)
fout << -distanta[y];
else
fout << distanta[y];
return 0;
}