#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 501;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

int n,m;
int p[N][N],dis[N][N];
pii pos[N*N];
int xi[4] = {-1,0,1,0},yi[4] = {0,1,0,-1};

queue<pii> q1;

inline void update(int x,int y){
   fr(i,0,3){
      int nx = x+xi[i],ny = y+yi[i];
      if(nx == 0 || nx == n+1 || ny == 0 || ny == n+1)continue ;
      if(dis[x][y]+(p[x][y] > 0) < dis[nx][ny]){
         dis[nx][ny] = dis[x][y]+(p[x][y] > 0);
         update(nx,ny);
      }
   }
   return ;
}

int main(){
   scanf("%d",&n);
   fr(i,1,n*n){
      int x;
      scanf("%d",&x);
      pos[i] = pii((x-1)/n+1,(x-1)%n+1);
   }

   fr(i,1,n)
      fr(j,1,n)p[i][j] = 1;
   fr(i,1,n)
      fr(j,1,n)dis[i][j] = min(min(i-1,j-1),min(n-i,n-j));

   int sum = 0;
   fr(i,1,n*n){
      sum += dis[pos[i].first][pos[i].second];
      p[pos[i].first][pos[i].second] = 0;
      update(pos[i].first,pos[i].second);
   }
   printf("%d\n",sum);
   return 0;
}
