#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;


#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(),a.end()

typedef tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
ll vis[501][501], done[501][501];
ll dx[] = {-1, 1, 0, 0};
ll dy[] = {0, 0, 1, -1};
int main(void)
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, i, j, ans = 0, k;
    cin>>n;
    ll init[n][n], cur;
    for(i = 0; i < n; i++) {
    	for(j = 0; j < n; j++) {
    		vis[i][j] = -1;
    		init[i][j] = min({i, n - i - 1, j, n - j - 1}) + 1;
    	}
    }
    // for(j = 0; j < n; j++) {
    // 		for(k = 0; k < n; k++) {
    // 			cout<<init[j][k]<<" ";
    // 		} cout<<"\n";
    // 	} 
    // 	cout<<"\n";
    queue<pair<ll, ll>> q;
    for(i = 0; i < n*n; i++) {
    	cin>>cur;
    	cur--;
    	ll row = cur / n;
    	ll col = cur % n;
    	done[row][col] = 1;
    	ans = ans + init[row][col] - 1;
    	init[row][col]--;
    	q.push(mp(row, col));
    	while(!q.empty()) {
    		pair<ll, ll> top = q.front();
    		q.pop();
    		for(j = 0; j < 4; j++) {
    			ll x = top.ff + dx[j];
    			ll y = top.ss + dy[j];
    			if(x >= 0 && x < n && y >= 0 && y < n && vis[x][y] != i) {
    				if(init[x][y] > init[top.ff][top.ss]) {
    					vis[x][y] = i;
    					init[x][y] = init[top.ff][top.ss];
    					if(!done[x][y]) {
    						init[x][y] += 1;
    					}
    					q.push(mp(x, y));
    				}
    			}
    		}
    	}
    	// for(j = 0; j < n; j++) {
    	// 	for(k = 0; k < n; k++) {
    	// 		cout<<init[j][k]<<" ";
    	// 	} cout<<"\n";
    	// }  cout<<"\n";
    	
    }
    cout<<ans<<"\n";
}	