#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<pair<int,int>>M[30001];
int dist[30001];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>q;
int n,m;
int x,y;
void dijk(int nod)
{
for(int i = 1;i<=n;++i)
dist[i] = 1e14;
dist[nod] = 0;
q.push({dist[nod],nod});
while(!q.empty())
{
int x = q.top().second;
q.pop();
for(auto i : M[x])
{
int vec = i.first;
int cost = i.second;
if(dist[vec] > dist[x] + cost)
{
dist[vec]= dist[x] + cost;
q.push({dist[vec],vec});
}
}
}
}
signed main() {
freopen ("sate.in","r",stdin);
freopen ("sate.out","w",stdout);
cin >> n >> m >> x >> y;
for(int i = 1;i<=m;++i)
{
int st,dr,c;
cin >> st >> dr >> c;
M[st].push_back({dr,c});
M[dr].push_back({st,-c});
}
dijk(x);
cout << dist[y];
return 0;
}