#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
vector<pair<int, int>> v[30005];
int poz[30005];
bool vizitat[30005];
int main() {
fin >> n >> m >> x >> y;
for (int i = 0; i < m; i++) {
int a, b, d;
fin >> a >> b >> d;
v[a].push_back({b, d});
v[b].push_back({a, -d});
}
queue<int> q;
vizitat[x] = true;
q.push(x);
while (!q.empty() && !vizitat[y]) {
int a = q.front();
q.pop();
for (pair<int, int> p : v[a]) {
int b = p.first;
if (!vizitat[b]) {
vizitat[b] = true;
poz[b] = poz[a] + p.second;
q.push(b);
}
}
}
if (poz[y] < 0)
fout << -poz[y] << '\n';
else
fout << poz[y] << '\n';
return 0;
}