#include <fstream>
#include <queue>
using namespace std;
class Edge {
public:
int to;
int cost;
Edge(int _to = 0, int _cost = 0) :
to(_to), cost(_cost) {}
};
const int N_MAX = 30001;
int n, m, _begin, _end;
vector<Edge> graph[N_MAX];
int dist[N_MAX];
bool visited[N_MAX];
queue<int> q;
int main() {
ifstream f("sate.in");
f >> n >> m >> _begin >> _end;
while(m--) {
int x, y, d;
f >> x >> y >> d;
graph[x].emplace_back(y, d);
graph[y].emplace_back(x, -d);
}
dist[_begin] = 0;
visited[_begin] = 1;
q.push(_begin);
while(!q.empty()) {
int crt = q.front();
q.pop();
for(auto edge : graph[crt]) {
if(!visited[edge.to]) {
visited[edge.to] = 1;
dist[edge.to] = dist[crt] + edge.cost;
q.push(edge.to);
}
}
}
ofstream("sate.out") << dist[_end] << '\n';
return 0;
}