#include <fstream>
#include <vector>
#include <queue>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
const int NMAX = 30005;
int n, m, x, y;
std::vector<std::pair<int, int>> graph[NMAX];
int dist[NMAX];
bool visited[NMAX];
void bfs() {
std::queue<int> q;
q.push(x);
visited[x] = true;
dist[x] = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
if (node == y)
return;
for (auto &edge : graph[node]) {
int next = edge.first;
if (!visited[next]) {
visited[next] = true;
dist[next] = dist[node] + edge.second;
q.push(next);
}
}
}
}
int main() {
fin >> n >> m >> x >> y;
for (int k = 0; k < m; k++) {
int i, j, d;
fin >> i >> j >> d;
graph[i].push_back({j, d});
graph[j].push_back({i, -d});
}
bfs();
int result = dist[y];
if (result < 0)
result = -result;
fout << result << '\n';
return 0;
}