#include <bits/stdc++.h>
using namespace std;
int main(){
int N; cin>>N;
int x[N][N]={};
int p,a,b;
int counter=0;
int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;
for(int i=0; i<N*N;i++){
cin>>p;
a=p/N;
b=p%N;
if(b==0){
x[a-1][N-1]=1;
continue;}
else if(a==0||a==N-1){
x[a][b-1]=1;
continue;
}
else{
x[a][b-1]=1;
for(int k=0; k<a;k++){
if(x[k][b-1]==0){
counter1++;
}
}
for(int k=a+1; k<N;k++){
if(x[k][b-1]==0){
counter2++;
}
}
counter1=min(counter1,counter2);
counter2=0;
for(int k=0; k<b-1;k++){
if(x[a][k]==0){
counter2++;
}
}
counter1=min(counter1,counter2);
counter2=0;
for(int k=b; k<N;k++){
if(x[a][k]==0){
counter2++;
}
}
counter1=min(counter1,counter2);
counter2=0;
counter+=counter1;
}
}
cout<<counter<<endl;}