#include <iostream>
#include<utility>
#include<vector>
#include<fstream>
using namespace std;
int N, M, x,y, i,j,D;
bool gasit = false;
vector<vector<pair<int, int>>> matrice;
vector<int> vizitat;
ofstream fout("sate.out");
int Dfs(int curent, int distanta){
if (vizitat[curent] != 0){ return 0;}
else { vizitat[curent] = 1;}
int dis_vecin;
for (int t = 0; t<matrice[curent].size(); t++) {
if (gasit){return 0;}
int vecin = matrice[curent][t].first;
if (vecin>curent){dis_vecin = distanta+matrice[curent][t].second;}
else{dis_vecin = distanta-matrice[curent][t].second;}
if (vecin == y) {
gasit = true;
cout<<dis_vecin;
return 0;}
else Dfs(vecin, dis_vecin);
}
return 0;}
int main() {
ifstream fin("sate.in");
fin>>N>>M>>x>>y;
matrice.resize(N+1);
for (int t =0; t<M; t++) {
fin>>i>>j>>D;
matrice[i].push_back({j, D});
matrice[j].push_back({i, D});
}
fin.close();
vizitat.resize(N+1);
vizitat[x] = 1;
for (int t=0; t<matrice[x].size(); t++) {
if (gasit) break;
if (matrice[x][t].first ==y){fout<<matrice[x][t].second; return 0;}
else {
if (matrice[x][t].first>x) Dfs(matrice[x][t].first, matrice[x][t].second);
else Dfs(matrice[x][t].first, - matrice[x][t].second);
}
}
return 0;
}