from collections import deque INF = 10 ** 18 N, M = map(int, input().split()) c = [[0] * N for _ in range(N)] for _ in range(M): h, w, cc = map(int, input().split()) c[h - 1][w - 1] = cc dp0 = [[(INF, 0)] * N for _ in range(N)] dp1 = [[(INF, 0)] * N for _ in range(N)] dp0[0][0] = (0, 0) q = deque([(0, 0, 0)]) while q: h, w, t = q.popleft() if t == 0: if h != 0: if dp0[h - 1][w] > dp0[h][w] + 1 + c[h - 1][w]: dp0[h - 1][w] = dp0[h][w] + 1 + c[h - 1][w] q.append((h - 1, w, 0)) if dp1[h - 1][w] > dp0[h][w] + 1: dp0[h - 1][w] = dp0[h][w] + 1 q.append((h - 1, w, 1)) if h != N - 1: if dp0[h + 1][w] > dp0[h][w] + 1 + c[h + 1][w]: dp0[h + 1][w] = dp0[h][w] + 1 + c[h + 1][w] q.append((h + 1, w, 0)) if dp1[h + 1][w] > dp0[h][w] + 1: dp0[h + 1][w] = dp0[h][w] + 1 q.append((h + 1, w, 1)) if w != 0: if dp0[h][w - 1] > dp0[h][w] + 1 + c[h][w - 1]: dp0[h][w - 1] = dp0[h][w] + 1 + c[h][w - 1] q.append((h, w - 1, 0)) if dp1[h][w - 1] > dp0[h][w] + 1: dp0[h][w - 1] = dp0[h][w] + 1 q.append((h, w - 1, 1)) if w != N - 1: if dp0[h][w + 1] > dp0[h][w] + 1 + c[h][w + 1]: dp0[h][w + 1] = dp0[h][w] + 1 + c[h][w + 1] q.append((h, w + 1, 0)) if dp1[h][w + 1] > dp0[h][w] + 1: dp0[h][w + 1] = dp0[h][w] + 1 q.append((h, w + 1, 1)) elif t == 1: if h != 0: if dp1[h - 1][w] > dp1[h][w] + 1 + c[h - 1][w]: dp1[h - 1][w] = dp1[h][w] + 1 + c[h - 1][w] q.append((h - 1, w, 0)) if h != N - 1: if dp1[h + 1][w] > dp1[h][w] + 1 + c[h + 1][w]: dp1[h + 1][w] = dp1[h][w] + 1 + c[h + 1][w] q.append((h + 1, w, 0)) if w != 0: if dp1[h][w - 1] > dp1[h][w] + 1 + c[h][w - 1]: dp1[h][w - 1] = dp1[h][w] + 1 + c[h][w - 1] q.append((h, w - 1, 0)) if w != N - 1: if dp1[h][w + 1] > dp1[h][w] + 1 + c[h][w + 1]: dp1[h][w + 1] = dp1[h][w] + 1 + c[h][w + 1] q.append((h, w + 1, 0)) print(min(dp0[N - 1][N - 1], dp1[N - 1][N - 1]))