from sys import stdin import heapq def Dijkstra(lis,start): ret = [float("inf")] * len(lis) ret[start] = 0 end_flag = [False] * len(lis) end_num = 0 q = [[0,start]] while len(q) > 0: ncost,now = heapq.heappop(q) if end_flag[now]: continue end_flag[now] = True end_num += 1 if end_num == len(lis): break for nex,ecost in lis[now]: if ret[nex] > ncost + ecost: ret[nex] = ncost + ecost heapq.heappush(q , [ret[nex] , nex]) return ret N,M = map(int,stdin.readline().split()) cost =[ [0] * N for i in range(N)] for loop in range(M): h,w,c = map(int,stdin.readline().split()) h -= 1 w -= 1 cost[h][w] += c ef = [ [ [float("inf"),float("inf")] for i in range(N)] for j in range(N) ] d = [ [ [False,False] for i in range(N)] for j in range(N) ] ef[0][0][0] = 0 q = [(0,0,0,0)] while len(q) > 0: ncost,ni,nj,nk = heapq.heappop(q) if ni == nj == N-1: break if d[ni][nj][nk]: continue d[ni][nj][nk] = True if ni != 0 and ef[ni-1][nj][nk] > ef[ni][nj][nk] + 1 + cost[ni-1][nj]: ef[ni-1][nj][nk] = ef[ni][nj][nk] + 1 + cost[ni-1][nj] heapq.heappush(q , (ef[ni-1][nj][nk],ni-1,nj,nk)) if ni != 0 and nk == 0 and ef[ni-1][nj][nk+1] > ef[ni][nj][nk] + 1: ef[ni-1][nj][nk+1] = ef[ni][nj][nk] + 1 heapq.heappush(q , (ef[ni-1][nj][nk+1],ni-1,nj,nk+1)) if ni != N-1 and ef[ni+1][nj][nk] > ef[ni][nj][nk] + 1 + cost[ni+1][nj]: ef[ni+1][nj][nk] = ef[ni][nj][nk] + 1 + cost[ni+1][nj] heapq.heappush(q , (ef[ni+1][nj][nk],ni+1,nj,nk)) if ni != N-1 and nk == 0 and ef[ni+1][nj][nk+1] > ef[ni][nj][nk] + 1: ef[ni+1][nj][nk+1] = ef[ni][nj][nk] + 1 heapq.heappush(q , (ef[ni+1][nj][nk+1],ni+1,nj,nk+1)) if nj != 0 and ef[ni][nj-1][nk] > ef[ni][nj][nk] + 1 + cost[ni][nj-1]: ef[ni][nj-1][nk] = ef[ni][nj][nk] + 1 + cost[ni][nj-1] heapq.heappush(q , (ef[ni][nj-1][nk],ni,nj-1,nk)) if nj != 0 and nk == 0 and ef[ni][nj-1][nk+1] > ef[ni][nj][nk] + 1: ef[ni][nj-1][nk+1] = ef[ni][nj][nk] + 1 heapq.heappush(q , (ef[ni][nj-1][nk+1],ni,nj-1,nk+1)) if nj != N-1 and ef[ni][nj+1][nk] > ef[ni][nj][nk] + 1 + cost[ni][nj+1]: ef[ni][nj+1][nk] = ef[ni][nj][nk] + 1 + cost[ni][nj+1] heapq.heappush(q , (ef[ni][nj+1][nk],ni,nj+1,nk)) if nj != N-1 and nk == 0 and ef[ni][nj+1][nk+1] > ef[ni][nj][nk] + 1: ef[ni][nj+1][nk+1] = ef[ni][nj][nk] + 1 heapq.heappush(q , (ef[ni][nj+1][nk+1],ni,nj+1,nk+1)) print (min(ef[N-1][N-1]))