import sys readline=sys.stdin.readline import heapq N,M=map(int,readline().split()) C=[[0]*N for h in range(N)] for m in range(M): h,w,c=map(int,readline().split()) h-=1;w-=1 C[h][w]=c inf=1<<60 dist0=[[inf]*N for h in range(N)] dist1=[[inf]*N for h in range(N)] dist0[0][0]=0 queue=[(0,0,0,0)] while queue: dx,bx,hx,wx=heapq.heappop(queue) if bx==0: if dist0[hx][wx]dx+C[hy][wy]+1: dist0[hy][wy]=dx+C[hy][wy]+1 heapq.heappush(queue,(dist0[hy][wy],bx,hy,wy)) if dist1[hy][wy]>dx+1: dist1[hy][wy]=dx+1 heapq.heappush(queue,(dist1[hy][wy],1,hy,wy)) else: if dist1[hy][wy]>dx+C[hy][wy]+1: dist1[hy][wy]=dx+C[hy][wy]+1 heapq.heappush(queue,(dist1[hy][wy],bx,hy,wy)) ans=dist1[N-1][N-1] print(ans)