N,M = map(int,input().split()) cost_s = [[0 for i in range(N)] for j in range(N)] cost_e = [[0 for i in range(N)] for j in range(N)] memo = [] for _ in range(M): h,w,c = map(int,input().split()) cost_s[h-1][w-1] = c cost_e[h-1][w-1] = c memo.append((h-1,w-1,c)) for i in range(N): for j in range(N): if i==0 and j==0: continue tmp = 10**18 if i!=0: tmp = cost_s[i-1][j] + 1 if j!=0: tmp = min(cost_s[i][j-1] + 1,tmp) cost_s[i][j] += tmp for i in range(N-1,-1,-1): for j in range(N-1,-1,-1): if i==N-1 and j==N-1: continue tmp = 10**18 if i!=N-1: tmp = cost_e[i+1][j] + 1 if j!=N-1: tmp = min(cost_e[i][j+1] + 1,tmp) cost_e[i][j] += tmp res = cost_s[N-1][N-1] for h,w,c in memo: tmp = cost_s[h][w] + cost_e[h][w] - 2*c res = min(res,tmp) print(res)