#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M, X, Y; cin >> N >> M >> X >> Y;
vector<vector<pair<int, int>>>G(N + 3);
for(int i = 1; i <= M; i++) {
int x, y, c; cin >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
vector<int>cost(N + 3, -1);
queue<int>q;
cost[X] = 0;
q.push(X);
while(!q.empty()) {
for(auto i : G[q.front()]) {
if(cost[i.first] == -1) {
if(q.front() < i.first)
cost[i.first] = cost[q.front()] + i.second;
else cost[i.first] = cost[q.front()] - i.second;
q.push(i.first);
}
}
q.pop();
}
// for(int i = 1; i <= N; i++) cout << cost[i] << ' ';
cout << cost[Y];
}