import heapq INF = 1 << 60 n, m = map(int, input().split()) cost = [[0 for _ in range(n)] for _ in range(n)] for _ in range(m): hi, wi, ci = map(int, input().split()) cost[hi - 1][wi - 1] = ci dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] dist = [[[INF for _ in range(2)] for _ in range(n)] for _ in range(n)] dist[0][0][0] = 0 hp = [(0, 0, 0, 0)] while len(hp) > 0: cd, ci, cx, cy = heapq.heappop(hp) if cd > dist[cx][cy][ci]: continue for d in range(4): nx = cx + dx[d] if nx < 0 or nx >= n: continue ny = cy + dy[d] if ny < 0 or ny >= n: continue if dist[nx][ny][ci] > dist[cx][cy][ci] + cost[nx][ny] + 1: dist[nx][ny][ci] = dist[cx][cy][ci] + cost[nx][ny] + 1 heapq.heappush(hp, (dist[nx][ny][ci], ci, nx, ny)) if ci == 0 and dist[nx][ny][1] > dist[cx][cy][ci] + 1: dist[nx][ny][1] = dist[cx][cy][ci] + 1 heapq.heappush(hp, (dist[nx][ny][1], 1, nx, ny)) print(min(dist[-1][-1]))