#include<fstream>
#include <vector>
#include <bitset>
#include <queue>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
int n, m, X, Y;
std::queue<std::pair<int, int>> q;
std::bitset<30005> viz;
std::vector<std::pair<int, int>> L[30005];
int main() {
int a, b, d;
fin >> n >> m >> X >> Y;
for (int i = 1; i <= m; i++) {
fin >> a >> b >> d;
L[a].push_back(std::make_pair(b, d));
L[b].push_back(std::make_pair(a, -d));
}
q.push(std::make_pair(X, 0));
viz[X] = 1;
while (!q.empty()) {
auto[k, d] = q.front();
// k = q.front().first
// d = q.front().second
q.pop();
for (auto& w : L[k]) {
if (viz[w.first] == 0) {
q.push(std::make_pair(w.first, d + w.second));
viz[w.first] = 1;
if (w.first == Y) {
fout << d + w.second << "\n";
return 0;
}
}
}
}
}