#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
vector<pair<int, int>> g[30005];
int dist[30005];
bool viz[30005];
void BFS(int start) {
queue<int> q;
q.push(start);
viz[start] = true;
dist[start] = 0;
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto vecin : g[nod]) {
int v = vecin.first;
int cost = vecin.second;
if (!viz[v]) {
viz[v] = true;
dist[v] = dist[nod] + cost;
q.push(v);
}
}
}
}
int main() {
fin >> n >> m >> x >> y;
for (int k = 0; k < m; k++) {
int a, b, d;
fin >> a >> b >> d;
g[a].push_back({b, d});
g[b].push_back({a, -d});
}
BFS(x);
fout << abs(dist[y]) << "\n";
return 0;
}