from collections import deque
def Solve():
graph = {}
with open("sate.in", "r") as file:
n, m, x, y = [int(x.strip()) for x in file.readline().strip().split(" ")]
graph = {x: [] for x in range(1, n + 1)}
for i in range(m):
i, j, d = [int(x.strip()) for x in file.readline().strip().split(" ")]
graph[i].append((j, d))
graph[j].append((i, -d))
queue = deque()
queue.append((x, 0))
visited = [0] * (n + 1)
visited[x] = 1
while queue:
candidate, distance = queue.popleft()
for neighbour in graph[candidate]:
if visited[neighbour[0]] == 0:
if neighbour[0] == y:
with open("sate.out", "w") as file:
file.write(str(abs(neighbour[1] + distance)))
return
else:
queue.append((neighbour[0], distance + neighbour[1]))
visited[neighbour[0]] = 1
if __name__ == "__main__":
Solve()