#include <bits/stdc++.h>
using namespace std;

vector<int> seat;
vector<bool> fix;
int Ns;
vector<int> movex={0,0,-1,1};
vector<int> movey={-1,1,0,0};

void optimize(int);



int main() {
  int N;
  cin >> N;
  Ns=N+2;
  int MN=N*N;
  int MN2=(N+2)*(N+2);
  seat=vector<int>(MN2);
  fix=vector<bool>(MN2,true);
  for(int i=0;i<N;i++){
   for(int j=0;j<N;j++){
    seat.at((i+1)*Ns+j+1)=min(min(i,N-i-1),min(j,N-j-1)); 
    
   }
  
  }
  
  
  
  int hate=0;
  int next;
  
  for(int i=0;i<MN;i++){
    cin >> next;
    next--;
    next=((next/N)+1)*Ns+(next%N)+1;
    
    hate+=seat.at(next);
    fix.at(next)=false;
    optimize(next);
    
    
  }
  
  cout << hate << endl;
  
  
}


void optimize(int N){
  int hit=seat.at(N)+fix.at(N);
  for(int i=0;i<4;i++){
    int around=movey.at(i)*Ns+movex.at(i)+N; 
    if (seat.at(around)>hit){
      seat.at(around)=hit;
      optimize(around);
    }
  }
  
}