// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp> // order_of_key
// #include <ext/pb_ds/tree_policy.hpp> // find by order
// using namespace __gnu_pbds; 
// #define ordered_set tree< int , null_type,less< int >, rb_tree_tag,tree_order_statistics_node_update>  

// const ll MAX_LL = 0xFFFFFFFFFFFFFFFF;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<ll>(a, b)(rng)

typedef long double             ld;
typedef long long               ll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ll>              vll;
typedef vector<vll>             vvll;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vii>             vvii;
#define pll pair<long long, long long>

#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template<class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<class T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const ll T = 1<<20;
const ld inf = 1e18;
int n, k;

int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};

int czas[600][600];
int koszt[600][600];
int odw[600][600];

ii numer[N];

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    vvi kiedy;
    int xd = 1;
    int xd2 = 1;
    for(int i = 1; i <= n; i++)
    	for(int j = 1; j <= n; j++){
    		numer[xd2] = {i, j};
    		xd2++;
    	}

    for(int i = 1; i <= n; i++)
    	for(int j = 1; j <= n; j++){
    		int temp;
    		cin >> temp;

    		kiedy.pb({xd, numer[temp].f, numer[temp].s});
    		xd++;
    	}
    sort(all(kiedy));

    for(int i = 1; i <= n; i++)
    	for(int j = 1; j <= n; j++)
    		koszt[i][j] = min({i, j, n - i + 1, n - j + 1});
    	
    int ans = 0;
    for(auto & u : kiedy){
    	ii pom = {u[1], u[2]};
    	int teraz = u[0];

    	koszt[pom.f][pom.s]--;
    	odw[pom.f][pom.s] = 1;
    	ans += koszt[pom.f][pom.s];
    	// cout << teraz << ' ' << "POLE " << pom.f << ' ' << pom.s << "       " << koszt[pom.f][pom.s] << endl;
    	// cout << koszt[pom.f][pom.s];

    	queue<ii> kol;
    	FOR(i, 4){
    		int x = pom.f + X[i];
    		int y = pom.s + Y[i];

    		kol.push({x, y});
    	}

    	while(sz(kol)){
    		pom = kol.front();
    		kol.pop();
    		if(min(pom.f, pom.s) == 0 or max(pom.f, pom.s) == n + 1)
    			continue;

    		FOR(i, 4){
    			int x = pom.f + X[i];
    			int y = pom.s + Y[i];

    			if(koszt[x][y] + (!odw[pom.f][pom.s]) < koszt[pom.f][pom.s]){
    				koszt[pom.f][pom.s]--;
    				FOR(j, 4){
    					int xx = pom.f + X[j];
    					int yy = pom.s + Y[j];
    					kol.push({xx, yy});
    				}
    				break;
    			}
    		}
    	}
    	// for(int i = 1; i <= 3; i++){
    	// 	for(int j = 1; j <= 3; j++){
    	// 		cout << koszt[i][j] << ' ';
    	// 	}
    	// 	cout << endl;
    	// }
    	// cout << endl;
    }
    cout << ans << '\n';
}   	

Measured memory_bytes: 21184