#include <bits/stdc++.h>
using namespace std;
int n,m,i,j,k,ans,x,y;
vector<pair<int,int>>g[30005];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
queue<int>q;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
cin>>n>>m>>x>>y;
while (m--)
{
int d;
cin>>i>>j>>d;
g[i].push_back({d,j});
g[j].push_back({d,i});
}
vector<int>dist(n+1,INT_MAX);
vector<bool>seen(n+1,false);
seen[x]=true;
dist[x]=0;
q.push(x);
while (!q.empty())
{
int u=q.front();
q.pop();
for (auto v : g[u])
{
if (!seen[v.second])
{
seen[v.second]=true;
if (u<v.second)
{
dist[v.second]=dist[u]+v.first;
}
else
{
dist[v.second]=dist[u]-v.first;
}
q.push(v.second);
}
}
}
cout<<abs(dist[y]);
}