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

int a[303030];
int st[303030];
int del[303030];
deque<int> Q;
void check(int nxt, int now)
{
	if(st[now] + (!del[now]) < st[nxt])
	{
		st[nxt] = st[now] + (!del[now]);
		if(del[nxt])
			Q.push_front(nxt);
		else
			Q.push_back(nxt);
	}
} 
int main()
{
	int N;
	scanf("%d", &N);
	for(int i = 0; i < N * N; i++)
	{
		scanf("%d", &a[i]);
		a[i]--;
		st[i] = min(min(i/N, N - 1 - i/N), min(i%N, N - 1 - i%N));
	}
	int ans = 0;
	for(int i = 0; i < N * N; i++)
	{
		ans += st[a[i]];
		del[a[i]] = 1;
		Q.push_back(a[i]);
		while(!Q.empty())
		{
			int now = Q.front();
			Q.pop_front();
			int nxt = -1;
			if(now % N > 0)
			{
				nxt = now - 1;
				check(nxt, now);
			}
			if(now % N < N - 1)
			{
				nxt = now + 1;
				check(nxt, now); 
			}
			if(now / N > 0)
			{
				nxt = now - N;
				check(nxt, now); 
			}
			if(now / N < N - 1)
			{
				nxt = now + N;
				check(nxt, now);
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}