#include <bits/stdc++.h>
using namespace std;
  
int main(){
  int n;
  cin >> n;
  int ans=0;
  int arr[n][n]={};
  int count = 1;
  for(int i = 0;i<n;i++){
    for(int j=0;j<n;j++){
      arr[i][j] = count;
      count++;
    }
  }
  int numarr[(n*n)+1];
  for(int h=0;h<=n*n;h++){
    numarr[h]=h;
  }
  int haters = 0;
  int t = n*n;
  while(t--){
    int x;
    cin >> x;
    //cout << " " << x;
    int r,c;
    if(x%n==0){
      c=n-1;
      r=(x/n)-1;
    }
    else{
      c = (x%n)-1;
      r = x/n;
    }
   // printf("\n");
 // cout << r << " " << c << endl;
  int minhaters = INT_MAX;
  int hcount = 0;
  if(r==0||r==n-1||c==0||c==n-1){
  haters = haters + 0;
  arr[r][c]=0;
  numarr[x]=0;
  }
  else{
    for(int i=x+n;i<n*n;i+=n){
    if(numarr[i]!=0){
      hcount++;
    }
    }
    minhaters = min(minhaters,hcount);
    hcount =0;
    for(int i=x-n;i>=0;i=i-n){
    if(numarr[i]!=0){
      hcount++;
    }
    }
    minhaters = min(minhaters,hcount);
    hcount =0;
    for(int i=x+1;i<=n*(r+1);i++){
    if(numarr[i]!=0){
      hcount++;
    }
    }
    minhaters = min(minhaters,hcount);
    hcount =0;
    for(int i=x-1;i>=(n*r)+1;i--){
    if(numarr[i]!=0){
      hcount++;
    }
    }
    minhaters = min(minhaters,hcount);
    haters = haters + minhaters;
     arr[r][c]=0;
  numarr[x]=0;
}
}
cout << haters << endl;
  return 0;
}