//2020年4月17日現在

#include<stdio.h>
#include<cmath>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<complex>
#include<queue>
#include<set>
#include<utility>
#include<iomanip>
#include<limits>
#include<thread>
#include<map>
#include<stack>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<tuple>
#include <cstdio>

using namespace std;

template <class... A>
void prints() { cout << "\n"; }
template <class... A>
void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &... rest) { cout << " " << first; prints_rest(rest...); }
template <class T, class... A>
void prints(const T &first, const A &... rest) { cout << first; prints_rest(rest...); }
template <class T, class... A>
void printd(const T &first, const A &... rest) { cout << fixed << setprecision(15) << first; prints_rest(rest...); }


namespace {
    // printElems(&a[0], &a[n]);　みたいな感じで使う。
    void printElems(const int* begin, const int* end)
    {
        for (const int* p = begin; p != end; ++p) {
            std::cout << *p << ' ';
        }
        std::cout << std::endl;
    }
}

#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1; i<n+1; i++)
#define repr(i,n) for(int i=n-1; i>=0; i--)
#define repr1(i,n) for(int i=n; i>0; i--)
#define FOR(i,j,n) for(int i=j; i < n; i++)
#define souatari(i,j,n) rep1(i,n-1)rep(j,i)
#define souatari1(i,j,n) for(int i=2; i<n+1; i++)rep1(j,i-1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int>  vi;
typedef vector<vi>  vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int,3> arr3;
typedef long double D;


const ll llinf =  numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
const ll BIG = 1LL<<60;

int plist[250005];
int sheet[505][505];
bool kuuseki[505][505];
bool donelist[505][505];

int main(){
	int n;
	cin >> n;
	int n2 = n*n;
	rep(i,n2){
		int tmp;
		cin >> tmp;
		plist[i] = tmp-1;
	}
	rep1(i,n)rep1(j,n){
		int tmp = inf;
		tmp = min(tmp,i-1);
		tmp = min(tmp,j-1);
		tmp = min(tmp,n-i);
		tmp = min(tmp,n-j);
		sheet[i][j] = tmp;
	}

	int ans = 0;
	rep(i,n+2) donelist[i][0] = true;
	rep(i,n+2) donelist[0][i] = true;
	rep(i,n+2) donelist[i][n+1] = true;
	rep(i,n+2) donelist[n+1][i] = true;


	int nx[4] = {1,0,-1,0};
	int ny[4] = {0,1,0,-1};

	rep(_,n2){
		//rep1(tmp1,n)rep1(tmp2,n) donelist[tmp1][tmp2] = false;
//		int tmp = plist[_];
		int fst,snd;
		fst = plist[_]/n+1;
		snd = plist[_]%n+1;
		ans+=sheet[fst][snd];
		kuuseki[fst][snd] = true;

		queue<arr3> q; // {x,y,距離}
		rep(i,4){
			if(sheet[fst+nx[i]][snd+ny[i]]>sheet[fst][snd]){
				sheet[fst+nx[i]][snd+ny[i]] = sheet[fst][snd];
				q.push({fst+nx[i],snd+ny[i],sheet[fst][snd]});
			}
		}
		while(!q.empty()){
			arr3 tmp = q.front();
			q.pop();
			int nextkyori = tmp[2];
			if(!kuuseki[tmp[0]][tmp[1]]) nextkyori++;
			rep(i,4){
				if(sheet[tmp[0]+nx[i]][tmp[1]+ny[i]]>nextkyori){
					sheet[tmp[0]+nx[i]][tmp[1]+ny[i]] = nextkyori;
					q.push({tmp[0]+nx[i],tmp[1]+ny[i],nextkyori});
				}
			}
		}
	};
	prints(ans);






	return 0;
}



Measured memory_bytes: 6092