#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAXN = 30005;
vector<pair<int, int>> adj[MAXN];
int dist[MAXN];
bool viz[MAXN];
int main() {
int n, m, x, y;
fin >> n >> m >> x >> y;
for (int i = 0; i < m; ++i) {
int u, v, d;
fin >> u >> v >> d;
if (u > v) swap(u, v);
adj[u].push_back({v, d});
adj[v].push_back({u, -d});
}
queue<int> q;
q.push(x);
viz[x] = true;
dist[x] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == y) {
fout << dist[y] << "\n";
return 0;
}
for (auto& edge : adj[u]) {
int v = edge.first;
int cost = edge.second;
if (!viz[v]) {
viz[v] = true;
dist[v] = dist[u] + cost;
q.push(v);
}
}
}
return 0;
}