#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int iINF = 2147483647 / 2;
const long long int llINF = 9223372036854775807 / 2;
using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
bool paircomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first == b.first) return a.second < b.second;
  return a.first < b.first;
}
ll POW(ll n, ll m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = POW(n, m / 2);
    return (tmp * tmp);
  } else {
    return (n * POW(n, m - 1));
  }
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll n;
ll f(ll x, ll y) { return y * n + x; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vl P(n * n);
  for (ll i = 0; i < (n * n); i++) {
    cin >> P[i];
    P[i]--;
  }
  vl sit(n * n, 1);
  vl cost(n * n, 0);
  for (ll i = 0; i < (n * n); i++) {
    ll y = i / n, x = i % n;
    cost[i] = (ll)min({y, n - 1LL - y, x, n - 1LL - x});
  }
  ll ans = 0;
  for (ll i = 0; i < (n * n); i++) {
    ans += cost[P[i]];
    priority_queue<ll, vector<ll>, greater<ll>> que;
    que.push(P[i]);
    sit[P[i]] = 0;
    while (!que.empty()) {
      ll q = que.top();
      que.pop();
      ll y = q / n, x = q % n;
      for (ll k = 0; k < (4); k++) {
        ll yy = y + dy[k], xx = x + dx[k];
        if (0 <= yy && yy < n && 0 <= xx && xx < n &&
            cost[f(yy, xx)] > cost[f(y, x)] + sit[f(y, x)]) {
          cost[f(yy, xx)] = cost[f(y, x)] + sit[f(y, x)];
          que.push(f(yy, xx));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
