#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("YES");}else{puts("NO");} #define MAX_N 200005 int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; int main() { int n, m; cin >> n >> m; ll a[n+2][n+2]; rep(i,n+2){ rep(j,n+2){ if(i==0||i==n+1||j==0||j==n+1) a[i][j] = 1001001001001001001; else a[i][j] = 0; } } rep(i,m){ int h, w, c; cin >> h >> w >> c; a[h][w] = c; } ll dp[n+2][n+2][2]; rep(i,n+2){ rep(j,n+2){ rep(k,2){ if(i==0||i==n+1||j==0||j==n+1) dp[i][j][k] = -1; else dp[i][j][k] = 1001001001001001001; } } } dp[1][1][0] = 0; pair p; priority_queue,vector>,greater>> que; p.first.first = 0; p.first.second = 0; p.second.first = 1; p.second.second = 1; que.push(p); while(!que.empty()){ p = que.top(); que.pop(); int x = p.second.first; int y = p.second.second; int k = p.first.second; if(p.first.first > dp[x][y][k]) continue; rep(i,4){ int nx = x + dx[i]; int ny = y + dy[i]; if(dp[x][y][k]+a[nx][ny]