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