#include <bits/stdc++.h>
 
using namespace std;
 
#define x first
#define y second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
 
const ll MOD = 1e9+7;
const ll INF = 1e9+5;
 
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
 
int n;
int d[505][505];
int ima[505][505];
vector <int> v;
bool de;
 
void dfs(int i, int j, int k) {
	if (i<0 or j<0 or i>=n or j>=n) return ;
	if (d[i][j] <= k) return ;
	d[i][j] = k;
	for (int s=0 ; s<4 ; s++) {
		int nx = i+dx[s];
		int ny = j+dy[s];
		dfs(nx, ny, k+ima[i][j]);
	}
}
 
int main() {
	scanf("%d", &n);
	for (int i=0 ; i<n ; i++) {
		for (int j=0 ; j<n ; j++) {
			int x;
			scanf("%d", &x);
			v.pb(--x);
			d[i][j] = min(min(i, n-i-1), min(j, n-j-1));
			ima[i][j] = 1;
		}
	}
	int sol = 0;
	for (int k=0 ; k<v.size() ; k++) {
		int i = v[k]/n;
		int j = v[k]%n;
		sol += d[i][j];
		ima[i][j] = 0;
		dfs(i, j, d[i][j]++);
	}
	printf("%d\n", sol);
	return 0;
}