#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
// first = target, second = distance
vector<pair<int, int>> A[30001];
int visited[30001];
int N, M, X, Y, total_dist = 0;
bool dfs(int k) {
int target, dist;
visited[k] = 1;
for (auto &p : A[k]) {
if (!visited[p.first]) {
target = p.first, dist = p.second;
dist = target > k ? dist : -dist;
total_dist += dist;
if (target == Y || dfs(target))
return true;
total_dist -= dist;
}
}
return false;
}
int main() {
fin >> N >> M >> X >> Y;
int x, y, d;
for (int i = 0; i < M; i++) {
fin >> x >> y >> d;
A[x].push_back({y, d});
A[y].push_back({x, d});
}
/*
DFS(start = X/Y)
+
If target > current => total_distance += d(target, current)
else if target < current -= d(target, current)
te intorci din nivelul dfs si !found_Y(/X) => total_distance -= d(target, current)
*/
if (dfs(X))
// daca start (X) > end (Y) => -distanta
fout << abs(total_dist);
return 0;
}