#include #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; const int dx[]={0,-1,0,1},dy[]={1,0,-1,0}; template struct edge{ int to; T wt; edge(int to,const T& wt):to(to),wt(wt){} }; template using weighted_graph=vector>>; template void add_directed_edge(weighted_graph& G,int u,int v,const T& wt){ G[u].emplace_back(v,wt); } template vector Dijkstra(const weighted_graph& G,int s){ constexpr T INF=numeric_limits::max(); int n=G.size(); vector d(n,INF); d[s]=0; priority_queue> Q; Q.emplace(0,s); while(!Q.empty()){ T d0=-Q.top().first; int u=Q.top().second; Q.pop(); if(d0>d[u]) continue; for(const auto& e:G[u]){ int v=e.to; if(d[v]>d[u]+e.wt){ d[v]=d[u]+e.wt; Q.emplace(-d[v],v); } } } return d; } const long long INF=1LL<<61; int main(){ int n,m; scanf("%d%d",&n,&m); vector cost(n,vector(n,0LL)); rep(i,m){ int x,y; lint c; scanf("%d%d%lld",&x,&y,&c); x--; y--; cost[x][y]=c; } weighted_graph G(n*n); rep(i,n) rep(j,n) rep(k,4) { int x=i+dx[k],y=j+dy[k]; if(0<=x && x