import sys input = sys.stdin.buffer.readline def MI(): return map(int, input().split()) dij = [(1, 0), (0, 1), (-1, 0), (0, -1)] from heapq import * inf = 10**16 n, m = MI() cc = [[0]*n for _ in range(n)] for _ in range(m): i, j, c = MI() cc[i-1][j-1] = c hp = [] dd = [[[inf]*2 for _ in range(n)] for _ in range(n)] dd[0][0][0] = 0 heappush(hp, (0, 0, 0, 0)) while hp: d, i, j, k = heappop(hp) if (i,j)==(n-1,n-1): print(d) exit() if d > dd[i][j][k]: continue for di, dj in dij: ni, nj = i+di, j+dj if ni < 0 or ni >= n or nj < 0 or nj >= n: continue nd = d+cc[ni][nj]+1 if nd < dd[ni][nj][k]: dd[ni][nj][k] = nd heappush(hp, (nd, ni, nj, k)) if k == 0 and d+1 < dd[ni][nj][1]: dd[ni][nj][1] = d+1 heappush(hp, (d+1, ni, nj, 1))