#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
int main(){
int N; cin >> N; int P[N*N], now[N][N]={}, ans=0; bool flg[N][N]={};
REP(i, N*N){ cin >> P[i]; P[i]--; }
REP(i, N){ REP(j, N){ now[i][j]=min(min(i, j), min(N-1-i, N-1-j)); } }

REP(i, N*N){ 
  int x=P[i]/N, y=P[i]%N, n=now[x][y]; ans+=now[x][y]; flg[x][y]=true;
  vector< vector<int> > V={{x, y, 0}};
  for(int k=0; k<V.size(); k++){
  int a=V[k][0], b=V[k][1], c=V[k][2];
  if(a>0 && now[a-1][b]>n+c){ now[a-1][b]--; if(flg[a-1][b]){ V.push_back({a-1, b, 0}); }else{ V.push_back({a-1, b, c+1}); } }
  if(b>0 && now[a][b-1]>n+c){ now[a][b-1]--; if(flg[a][b-1]){ V.push_back({a, b-1, 0}); }else{ V.push_back({a, b-1, c+1}); } }
  if(a<N-1 && now[a+1][b]>n+c){ now[a+1][b]--; if(flg[a+1][b]){ V.push_back({a+1, b, 0}); }else{ V.push_back({a+1, b, c+1}); } }
  if(b<N-1 && now[a][b+1]>n+c){ now[a][b+1]--; if(flg[a][b+1]){ V.push_back({a, b+1, 0}); }else{ V.push_back({a, b+1, c+1}); } }
  }
}
cout << ans << "\n";
}
