#include <bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector<pair<int,int>>adj[100001];
queue<int>q;
int viz[100001];
int main()
{
int n,m,start,stop,j,i,p,c,total = 0,x,y;
f >> n >> m >> start >> stop;
for(i = 1; i <= m; i++)
{
f >> x >> y >> c;
adj[x].push_back({y,c});
adj[y].push_back({x,c});
}
q.push(start);
viz[start] = 1;
while(!q.empty())
{
int nod_curent = q.front();
viz[nod_curent] = 1;
q.pop();
for(i = 0; i < adj[nod_curent].size(); i++)
{
int vecin = adj[nod_curent][i].first;
if(viz[vecin] == 0)
{
viz[vecin] = 1;
if(vecin > nod_curent)
total = total + adj[nod_curent][i].second;
else
total = total - adj[nod_curent][i].second;
q.push(vecin);
}
if(vecin == stop)
{
total = total + adj[nod_curent][i].second;
g << total;
return 0;
}
}
}
//g << total;
return 0;
}