#include <cstdio>
#include <vector>
#include <queue>

int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};

int main()
{
  int n;
  scanf("%d",&n);
  std::vector<int> P(n*n);
  for(int i=0;i<n*n;i++){
    scanf("%d",&P[i]);
    P[i]--;
  }
  std::vector<int> d(n*n);
  std::vector<bool> s(n*n,true);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      d[i*n+j]=std::min(std::min(i,n-i-1),std::min(j,n-j-1))+1;
    }
  }
  int ans=0;
  for(int t=0;t<n*n;t++){
    int p=P[t];
    ans+=d[p]-1;
    d[p]--;
    s[p]=false;
    std::queue<int> Q;
    Q.push(p);
    while(!Q.empty()){
      int i=Q.front()/n,j=Q.front()%n;
      Q.pop();
      for(int v=0;v<4;v++){
	int ni=i+dx[v],nj=j+dy[v];
	if(ni<0||n<=ni||nj<0||n<=nj)continue;
	int nd=d[n*i+j]+(s[n*ni+nj]?1:0);
	if(d[n*ni+nj]>nd){
	  d[n*ni+nj]=nd;
	  Q.push(n*ni+nj);
	}
      }
    }
  }
  printf("%d\n",ans);
  return 0;
}
