from heapq import heappush,heappop INF = int(1e15) dh = [1,0,-1,0] dw = [0,1,0,-1] N, M = map(int,input().split()) dist0 = [[INF for j in range(N)] for i in range(N)] dist1 = [[INF for j in range(N)] for i in range(N)] c = [[0 for j in range(N)] for i in range(N)] dist0[0][0] = 0 dist1[0][0] = 0 for i in range(M): H, W, C = map(int,input().split()) H -= 1 W -= 1 c[H][W] = C q = [(0,0,0,0)] while q: C, H, W, used = heappop(q) if(used == 0 and dist0[H][W] < C): continue if(used == 1 and dist1[H][W] < C): continue for i in range(4): nH = H + dh[i] nW = W + dw[i] if(nH < 0 or nH >= N): continue if(nW < 0 or nW >= N): continue nC0 = C + c[nH][nW] + 1 nC1 = C + 1 if(used == 0): if(dist0[nH][nW] > nC0): heappush(q,(nC0,nH,nW,0)) dist0[nH][nW] = nC0 if(dist1[nH][nW] > nC1): heappush(q,(nC1,nH,nW,1)) dist1[nH][nW] = nC1 else: if(dist1[nH][nW] > nC0): heappush(q,(nC0,nH,nW,1)) dist1[nH][nW] = nC0 print(min(dist0[N-1][N-1],dist1[N-1][N-1]))