#include #include using namespace std; int N,M; int c[500][500]; long dist[2][500][500]; int d[5]={0,1,0,-1}; main() { cin>>N>>M; for(int i=0;i>x>>y; cin>>c[x-1][y-1]; } for(int i=0;i > > >P; for(int i=0;i<2;i++) { dist[i][0][0]=0; P.push(make_pair(0,make_pair(i,make_pair(0,0)))); } while(!P.empty()) { long cost=-P.top().first; int f=P.top().second.first,x=P.top().second.second.first,y=P.top().second.second.second; P.pop(); if(dist[f][x][y]=N||ty>=N)continue; long nxt=cost+c[tx][ty]+1; if(dist[f][tx][ty]>nxt) { dist[f][tx][ty]=nxt; P.push(make_pair(-nxt,make_pair(f,make_pair(tx,ty)))); } if(f==0) { nxt-=c[tx][ty]; if(dist[f+1][tx][ty]>nxt) { dist[f+1][tx][ty]=nxt; P.push(make_pair(-nxt,make_pair(f+1,make_pair(tx,ty)))); } } } } cout<