#include <bits/stdc++.h>

using namespace std;

#define sz(c) (int)c.size()
#define	pb push_back

typedef long long ll;
typedef pair<ll, ll> state;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;

constexpr int MAX_N = 510;
constexpr ll INF = 1000'000'000'000'000'000;
constexpr ll P = 9'982'443'53;
constexpr int SIGMA = 28;
constexpr double PI = 3.14159265359;
constexpr double toRad = PI/180.;

int dist[MAX_N][MAX_N];
bool present[MAX_N][MAX_N];
void decrease(int i, int j, int N){
	for(int z = i,notPresent = 0; z >= 1; z--){
		notPresent += present[z][j];
		dist[z][j] = min(dist[z][j], notPresent + dist[i][j]);		
	}

	for(int z = i, notPresent = 0; z <= N; z++){
		notPresent += present[z][j];
		dist[z][j] = min(dist[z][j], notPresent + dist[i][j]);
	}

	for(int z = j, notPresent = 0; z <= N; z++){
		notPresent += present[i][z];
		dist[i][z] = min(dist[i][z], notPresent + dist[i][j]);		
	}

	for(int z = j, notPresent = 0; z >= 1; z--){
		notPresent += present[i][z];
		dist[i][z] = min(dist[i][z], notPresent + dist[i][j]);
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	for(int i = 0; i < MAX_N; i++){
		for(int j = 0; j < MAX_N; j++){
			present[i][j] = true;
		}
	}
	
	int N;
	cin >> N;

	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			dist[i][j] = min(i-1,j-1);
			dist[i][j] = min(dist[i][j], N-i);
			dist[i][j] = min(dist[i][j], N-j);
		}
	}

	int tot = 0;
	for(int k = 0, p; k < N*N; k++){
		cin >> p;

		int i = p % N ? p/N + 1 : (p / N);
		
		int r = p % N;
		int j = (r == 0) ? N : r;

		//cout << i << "," << j << "\n";
		present[i][j] = false; 
		int mn = N;
		for(int z = i,notPresent = 0; z >= 1; z--){
			notPresent += present[z][j];
			mn = min(mn,notPresent + dist[z][j]);
		}

		for(int z = i, notPresent = 0; z <= N; z++){
			notPresent += present[z][j];
			mn = min(mn,notPresent + dist[z][j]);
		}

		for(int z = j, notPresent = 0; z <= N; z++){
			notPresent += present[i][z];
			mn = min(mn,notPresent + dist[i][z]);
		}

		for(int z = j, notPresent = 0; z >= 1; z--){
			notPresent += present[i][z];
			mn = min(mn,notPresent + dist[i][z]);
		}

		//cout << mn << "\n";
		tot += mn;
		dist[i][j] = mn;
		decrease(i,j,N);
	}
	cout << tot << "\n";
	return 0;
}
