from collections import deque from collections import defaultdict N, M = map(int, input().split()) d = defaultdict(int) s = set() for i in range(M): h, w, c = map(int, input().split()) h-=1;w-=1 s.add((h, w)) d[str(h)+'_'+str(w)] = c cost = [[[float('inf'), 0] for _ in range(N)] for _ in range(N)] cost[0][0][0] = 0 q = deque() q.append((0, 0)) xx = [0,1,0,-1] yy = [1,0,-1,0] while q: x, y = q.popleft() for px, py in zip(xx, yy): cx = x+px cy = y+py if 0<=cxcost[x][y][0]+1+c-max(cost[x][y][1], c): cost[cx][cy][0] = cost[x][y][0]+1+c cost[cx][cy][1] = max(cost[x][y][1], c) q.append((cx, cy)) else: if cost[cx][cy][0]-cost[cx][cy][1]>cost[x][y][0]+1-cost[x][y][1]: cost[cx][cy][0] = cost[x][y][0]+1 cost[cx][cy][1] = cost[x][y][1] q.append((cx, cy)) print(cost[-1][-1][0]-cost[-1][-1][1])