from heapq import heappop, heappush
import sys
N = int(sys.stdin.readline())
M = int(sys.stdin.readline())
graph = [[] for _ in range(N+1)]
for _ in range(M):
start, end, cost = map(int, sys.stdin.readline().split())
graph[start].append((end, cost))
start, end = map(int, input().split())
distance = [int(1e9) ]*(N+1)
def dijkstra(start):
heap = []
heappush(heap, (0, start))
distance[start] = 0
while heap:
dist, now = heappop(heap)
if distance[now] < dist: continue
for nxtCity, nxtCost in graph[now]:
updCost = nxtCost + dist
if updCost < distance[ nxtCity ]:
distance[nxtCity] = updCost
heappush(heap, (updCost, nxtCity))
dijkstra(start)
print(distance[end])