N,M = map(int,input().split()) G = [[1] * N for i in range(N)] G[0][0] = 0 for _ in range(M): h,w,c = map(int,input().split()) G[h - 1][w - 1] += c import heapq as hq q = [] hq.heappush(q, (0, 0, 0, 0)) visited = [[[False] * N for i in range(N)] for used in range(2)] cases = ((1,0),(0,1),(-1,0),(0,-1)) while q: c, used, i, j = hq.heappop(q) if visited[used][i][j]: continue visited[used][i][j] = True if i == N - 1 and j == N - 1: print(c) break for ci,cj in cases: ni = i + ci nj = j + cj if not (0 <= ni < N and 0 <= nj < N): continue if G[ni][nj] > 1 and used == 0: # まだ権利を使っていない if not visited[used + 1][ni][nj]: hq.heappush(q, (c + 1, used + 1, ni, nj)) if not visited[used][ni][nj]: hq.heappush(q, (c + G[ni][nj], used, ni, nj)) else: if not visited[used][ni][nj]: hq.heappush(q, (c + G[ni][nj], used, ni, nj))