import sys input = sys.stdin.readline N,M=map(int,input().split()) DP=[[1<<60]*N for i in range(N)] DP2=[[1<<60]*N for i in range(N)] MAP=[[1]*N for i in range(N)] for i in range(M): h,w,c=map(int,input().split()) h-=1 w-=1 MAP[h][w]+=c DP[0][0]=0 import heapq # Q=[(c,x,y,f)] Q=[(0,0,0,0)] while Q: c,x,y,f=heapq.heappop(Q) if f==0 and c>DP[x][y]: continue if f==1 and c>DP2[x][y]: continue if f==1: for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]: if 0<=z