//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007 

template <typename T>
istream& operator>>(std::istream& is, std::vector<T>& vec) {
    for (T& x: vec) { is >> x; }
    return is;
}

//二次元平面を探索しよう
template <typename T,typename U>                                                   
pair<T,U> operator+(const pair<T,U> & l,const pair<T,U> & r) {   
    return {l.first+r.first,l.second+r.second};                                    
} 

vector<pair<int, int>> nei;
void nei_init(){
	nei.push_back(make_pair(-1,0));nei.push_back(make_pair(1,0));nei.push_back(make_pair(0,-1));nei.push_back(make_pair(0,1));
	//4近傍ならコメントアウト
	//nei.push_back(make_pair(-1,-1));nei.push_back(make_pair(1,-1));nei.push_back(make_pair(1,1));nei.push_back(make_pair(-1,1));
}

bool is_safe(pair<int, int> p, int h, int w){
	if(p.first < 0 || p.second < 0) return false;
	if(p.first >=h || p.second >=w) return false;
	else true;
}
//

int main() {
	ll ans = 0;
	ll tmp;
	int n; cin >> n;
	int h[n][n] = {};
	REP(i, n){
		REP(j, n){
			h[i][j] = min({i, j, n-1-i, n-1-j});
		}
	}
	int p[n][n];
	REP(i, n) REP(j, n) p[i][j] = 1;
	nei_init();
	REP(co, n*n){
		cin >> tmp;
		int x = (tmp-1) / n;
		int y = (tmp-1) % n;
		ans += h[x][y];
		queue<pair<int, int>> q;
		q.push(make_pair(x, y));
		p[x][y] = 0;
		while(!q.empty()){
			pair<int, int> now = q.front();
			q.pop();
			//cout << i << " " << now.first << " " << now.second << " " << h[now.first][now.second] << endl;
			REP(fo, 4){
				//cout << (now+nei[fo]).first << " " << (now+nei[fo]).second << endl;
				if(is_safe(now + nei[fo], n, n) && h[(now+nei[fo]).first][(now+nei[fo]).second] > h[now.first][now.second] + p[now.first][now.second]){
					h[(now+nei[fo]).first][(now+nei[fo]).second] = h[now.first][now.second] + p[now.first][now.second];
					q.push(now+nei[fo]);
				}
			}
		}
	}
	cout << ans << endl;
}