#include <bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
const int NMAX = 30005;
vector<int>dist;
bitset<NMAX>used;
vector<vector<pair<int, int>>> graph;
int n, m, x, y;
void bfs(int node)
{
queue<int>q;
q.push(node);
dist[node]=0;
used[node]=1;
while(!q.empty())
{
int current =q.front();
q.pop();
for(auto coord:graph[current]) {
int next = coord.first;
int distance = coord.second;
if (!used[next]) {
dist[next]=dist[current] + distance;
used[next]=1;
q.push(next);
}
}
}
}
int main() {
f >> n >> m >> x >> y;
dist.assign(n+1,0);
graph.assign(n + 1,vector<pair<int, int>>());
for(int i=1; i<=m; i++)
{
int x, y, d;
f>>x>>y>>d;
graph[x].push_back({y, d});
graph[y].push_back({x, -d});
}
bfs (x);
g << abs(dist[y]) << endl;
return 0;
}