#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n, m, X, Y;
vector<pair<int, int>> adj[30005];
int pos[30005];
bool visited[30005];
void bfs(int s){
queue<int> q;
q.push(s);
visited[s] = true;
pos[s] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto e : adj[u])
{
int v = e.first;
int w = e.second;
if (!visited[v])
{
visited[v] = true;
pos[v] = pos[u] + w;
q.push(v);
}
}
}
}
int main(){
ifstream fin("sate.in");
ofstream fout("sate.out");
fin >> n >> m >> X >> Y;
for (int i = 0; i < m; i++){
int a, b, d;
fin >> a >> b >> d;
adj[a].push_back({b, d});
adj[b].push_back({a, -d});
}
bfs(X);
fout << abs(pos[Y]) << endl;
return 0;
}