#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int NMAX = 3e4+10;
const int INF = 2e9;
vector <vector <pair <int, pair<int, int>>>> nodes(NMAX);
long long dist[NMAX];
bool processed[NMAX];
void dfs (int a);
int main()
{
ifstream cin ("sate.in");
ofstream cout ("sate.out");
int n, m, st, nd, x, y, lgth;
cin >> n >> m >> st >> nd;
if (st > nd) swap(st, nd);
for (int i=0; i<m; i++)
{
cin >> x >> y >> lgth;
if (x > y) swap(x, y);
nodes[x].push_back({y, {lgth, 1}});
nodes[y].push_back({x, {lgth, -1}});
}
dfs(st);
cout << dist[nd] << '\n';
return 0;
}
void dfs (int a)
{
if (processed[a]) return;
processed[a] = true;
for (auto it : nodes[a])
{
dist[it.first] = dist[a] + (it.second.first * it.second.second);
dfs(it.first);
}
}