#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
int n;
int arr[250005];
int grid[505][505];
int dist[505][505];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pi> q;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	REP(i, 0, n*n){
		cin >> arr[i];
		arr[i]--;
		dist[i/n][i%n] = min(min(i/n, i%n), min(n - 1 - (i/n), n - 1 - (i%n)));
		grid[i/n][i%n] = 1;
	}
	ll answer = 0;
	REP(i, 0, n*n){
		answer += dist[arr[i]/n][arr[i]%n];
		grid[arr[i]/n][arr[i]%n] = 0;
		q.push(MP(arr[i]/n, arr[i]%n));
		while (!q.empty()){
			pi cur = q.front();
			q.pop();
			int xx = cur.first;
			int yy = cur.second;
			REP(j, 0, 4){
				int newx = xx + dx[j];
				int newy = yy + dy[j];
				if (newx < 0 || newx >= n) continue;
				if (newy < 0 || newy >= n) continue;
				if (dist[newx][newy] > dist[xx][yy] + grid[xx][yy]){
					dist[newx][newy] = dist[xx][yy] + grid[xx][yy];
					q.push(MP(newx, newy));
				}
			}
		}
	}
	cout << answer << "\n";
	
}