#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>
#include<chrono>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};

int N;
vector<vector<int>> d;
vector<vector<int>> ex;

void dijkstra(int sy, int sx, int sd){
	// priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> Q;
	queue<pair<int,pair<int,int>>> Q0, Q1;
	bool flag = true;
	d[sy][sx] = sd;
	
	Q0.push({sd,{sy, sx}});
	
	
	while(!Q0.empty() || !Q1.empty()){
		
		pair<int,pair<int,int>> p;
		
		if(flag) {
			if(Q0.size()) { //cout<<"<><>"<<endl;
				p = Q0.front();Q0.pop();
			} else {
				flag = !flag;
				continue;
			}
		} else {
			if(Q1.size()) {
				p = Q1.front();Q1.pop();
			} else {
				flag = !flag;
				continue;
			}
		}
		
		int cost = p.first;
		int vx = p.second.second;
		int vy = p.second.first;
		bool flag = false;
		
		// cout<<"vy = "<<vy<<" vx = "<<vx<<" cost = "<<cost<<" d = "<<d[vy][vx]<<endl;
		
		if(d[vy][vx] < cost) continue;
		
		// if(vx < 1 || vx > N || vy < 1 || vy > N) {
		if(ex[vy][vx]) {
			flag = true;
		}
		
		for(int k = 0; k < 4; k++){
			int ny = vy + dy[k], nx = vx + dx[k];
			int nc = 1;
			// cout<<"k = "<<k<<endl;
			// cout<<"ny = "<<ny<<" nx = "<<nx<<endl;
			if(nx < 0 || nx > N+1 || ny < 0 || ny > N+1) {
				continue;
			}
			// cout<<"ny = "<<ny<<" nx = "<<nx<<endl;
			if(ex[ny][nx]) {
				if(flag) nc = 0;
			}
			
			if(ex[ny][nx]) nc = 0;
			
			// cout<<"ny = "<<ny<<" nx = "<<nx<<endl;
			
			if(d[ny][nx] > cost + nc) {
				d[ny][nx] = cost + nc;
				
				if(flag) {
					if(nc == 0) Q0.push({d[ny][nx], {ny, nx}});
					else Q1.push({d[ny][nx], {ny, nx}});
				} else {
					if(nc == 1) Q0.push({d[ny][nx], {ny, nx}});
					else Q1.push({d[ny][nx], {ny, nx}});
				}
				
			}
		}
	}	
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int ans = 0;
	vector<int> P;
	
	cin>>N;
	
	// std::chrono::system_clock::time_point  start, end; // 型は auto で可
	// start = std::chrono::system_clock::now(); // 計測開始時間
	
	P.resize(N * N);
	
	d.resize(N+2, vector<int>(N+2, INF));
	ex.resize(N+2, vector<int>(N+2, false));
	
	for(int j = 0; j < d.size(); j++){
		for(int k = 0; k < d[j].size(); k++){
			if(j == 0 || j == N + 1 || k == 0 || k == N + 1) {
				ex[j][k] = true;
			}
		}
	}
	
	dijkstra(0, 0, 0);
	
	// for(int j = 0; j < d.size(); j++){
		// for(int k = 0; k < d[j].size(); k++){
			
			
			// cout<<d[j][k]<<" ";
		// }
		// cout<<endl;
	// }
	
	for(int i = 0; i < P.size(); i++){
		cin>>P[i];
		// P[i] = i+1;
		// P[i] = (i + N * 3 + 20) % P.size() + 1;
		
		int y, x;
		
		y = (P[i]-1)/N + 1;
		x = (P[i]-1)%N + 1;
		
		ans += d[y][x]-1;
		
		// cout<<y<<" "<<x<<endl;
		
		int minimum = INF;
		
		for(int k = 0; k < 4; k++){
			int ny = y + dy[k], nx = x + dx[k];
			
			minimum = min(d[ny][nx], minimum);
		}
		ex[y][x] = true;
		
		dijkstra(y, x, minimum);
		
		// cout<<endl;
			// for(int j = 0; j < d.size(); j++){
				// for(int k = 0; k < d[j].size(); k++){
					
					
					// cout<<d[j][k]<<" ";
				// }
				// cout<<endl;
			// }
			// cout<<"ans = "<<ans<<endl;
	}
	
	cout<<ans<<endl;
	
	// end = std::chrono::system_clock::now();  // 計測終了時間
	// double elapsed = chrono::duration_cast<std::chrono::milliseconds>(end-start).count(); //処理に要した時間をミリ秒に変換
	
	// cout<<elapsed<<"ms"<<endl;
	
	return 0;
}

Measured memory_bytes: 9744