from heapq import heappop,heappush def main1(n,m,hwc): # d0[i][j]:(0,0)から(i,j)への最小コスト # d1[i][j]:(i,j)から(n-1,n-1)への最小コスト cost=[0]*(n**2) for h,w,c in hwc: h,w=h-1,w-1 cost[h*n+w]=c inf=float('inf') d0=[inf]*(n**2) d0[0]=0 todo=[[0,0]] while todo: c,v=heappop(todo) if d0[v]c+dc: d0[nx*n+ny]=c+dc heappush(todo,[c+dc,nx*n+ny]) d1=[inf]*(n**2) d1[(n-1)*n+n-1]=0 todo=[[0,(n-1)*n+n-1]] while todo: c,v=heappop(todo) if d1[v]c+dc: d1[nx*n+ny]=c+dc heappush(todo,[c+dc,nx*n+ny]) ans=inf for i in range(n**2): ans=min(ans,d1[i]+d0[i]-2*cost[i]) return ans+2*(n-1) if __name__=='__main__': n,m=map(int,input().split()) hwc=[list(map(int,input().split())) for _ in range(m)] ret0=main0(n,m,hwc) print(ret0)