#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N=505;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int n,f[N][N],ans=0;
int vis[N][N];
void dfs(int x,int y) {
  int ret=f[x][y]+vis[x][y];
  for (int i=0;i<4;++i) {
    int nx=x+dx[i],ny=y+dy[i];
    if(ret<f[nx][ny]) {
      f[nx][ny]=ret;
      dfs(nx,ny);
    }
  }
  return ;
}
int main() {
  scanf("%d",&n);
  for (int i=1;i<=n;++i) {
    for (int j=1;j<=n;++j) {
      vis[i][j]=1;
      f[i][j]=min(min(i,n+1-i),min(j,n+1-j))-1;
    }
  }
  for (int i=1;i<=n*n;++i) {
    int s,x,y;
    scanf("%d",&s);
    x=(s+n-1)/n;
    y=s%n;
    if(y==0) y=n;
    ans+=f[x][y];
    vis[x][y]=0;
    dfs(x,y);
  }
  printf("%d\n",ans);
  return 0;
}
