#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, X, Y, d[30001];
bool v[30001];
vector<pair<int, int>> L[30001];
int main()
{
int i, j, D;
queue<int> q;
fin >> n >> m >> X >> Y;
while (m--) {
fin >> i >> j >> D;
if (i > j) {
swap(i, j);
}
L[i].push_back({j, D});
L[j].push_back({i, -D});
}
q.push(X);
while (!q.empty()) {
i = q.front();
q.pop();
if (i == Y) {
fout << d[Y] << "\n";
return 0;
}
if (v[i]) continue;
v[i] = 1;
for (auto k : L[i]) {
if (!v[k.first]) {
d[k.first] = d[i] + k.second;
q.push(k.first);
}
}
}
return 0;
}