#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MN = 500+5;

int D[MN][MN];
bool ch[MN][MN];

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0);
    int N;
    cin >> N;
    for(int i=1; i<=N; i++){
    	for(int j=1; j<=N; j++){
    		D[i][j] = min(min(i-1,N-i),min(j-1,N-j));
       	}
    }
    int ans = 0,x,y;
    queue<pii> Q;
    int c;
    for(int i=1; i<=N*N; i++){
    	cin >> c;
    	x = (c+N-1)/N;
    	y = (c-1)%N+1;
    	ans += D[x][y];
    	ch[x][y] = 1;
    	Q.push({x,y});
    	while(Q.size()){
    		tie(x,y) = Q.front();
    		Q.pop();
    		int d = D[x][y];
    		if(!ch[x][y]) d++;
    		if(D[x-1][y]>d){
    			D[x-1][y] = d;
    			Q.push({x-1,y});
    		}
    		if(D[x+1][y]>d){
    			D[x+1][y] = d;
    			Q.push({x+1,y});
    		}
    		if(D[x][y-1]>d){
    			D[x][y-1] = d;
    			Q.push({x,y-1});
    		}
    		if(D[x][y+1]>d){
    			D[x][y+1] = d;
    			Q.push({x,y+1});
    		}
    	}
    }
    cout << ans;
}
