#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){
int dis_vecin;
for (int t = 0; t<matrice[curent].size(); t++) {
int vecin = matrice[curent][t].first;
if (vizitat[vecin] != 0){ continue;}
else { vizitat[vecin] = 1;}
if (vecin>curent){dis_vecin = distanta+matrice[curent][t].second;}
else{dis_vecin = distanta-matrice[curent][t].second;}
if (vecin == y) {
gasit = true;
fout<<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);
for (int t = 0; t<=N; t++){vizitat[t] = 0;}
vizitat[x] = 1;
for (int t=0; t<matrice[x].size(); t++) {
if (gasit) break;
else Dfs(matrice[x][t].first, matrice[x][t].second);}
return 0;
}