#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ul = unsigned long;
using ull = unsigned long long;
using Graph = vector<vector<long>>;
inline vector<long> BFS(Graph _g, long start) {
  vector<long> _ans(_g.size(), -1);
  _ans[start] = 0;
  queue<long> _que;
  _que.push(start);
  while (!_que.empty()) {
    long _target = _que.front();
    _que.pop();
    for (long _towhere : _g[_target]) {
      if (_ans[_towhere] == -1) {
        _ans[_towhere] = _ans[_target] + 1;
        _que.push(_towhere);
      }
    }
  }
  return _ans;
}
ll getId(ll x, ll y, ll n) { return x + y * n; }
ull ans = 0;
int DIST[500 * 500];
int GONE[500 * 500];
ll N;
void change(ll Pi) {
  ll distPi = DIST[Pi];
  GONE[Pi] = 1;
  int minmin = distPi;
  if (Pi >= N) {
    minmin = min(DIST[Pi - N] + 1, minmin);
  }
  if (Pi % N != 0) {
    minmin = min(DIST[Pi - 1] + 1, minmin);
  }
  if (Pi < N * N - N) {
    minmin = min(DIST[Pi + N] + 1, minmin);
  }
  if (Pi % N != N - 1) {
    minmin = min(DIST[Pi + 1] + 1, minmin);
  }
  distPi = minmin;
  DIST[Pi] = minmin;
  if (Pi >= N) {
    if (DIST[Pi - N] > distPi) {
      DIST[Pi - N] = distPi;
      if (GONE[Pi - N] == 1) {
        change(Pi - N);
      }
    }
  }
  if (Pi % N != 0) {
    if (DIST[Pi - 1] > distPi) {
      DIST[Pi - 1] = distPi;
      if (GONE[Pi - 1] == 1) {
        change(Pi - 1);
      }
    }
  }
  if (Pi < N * N - N) {
    if (DIST[Pi + N] > distPi) {
      DIST[Pi + N] = distPi;
      if (GONE[Pi + N] == 1) {
        change(Pi + N);
      }
    }
  }
  if (Pi % N != N - 1) {
    if (DIST[Pi + 1] > distPi) {
      DIST[Pi + 1] = distPi;
      if (GONE[Pi + 1] == 1) {
        change(Pi + 1);
      }
    }
  }
}
int main() {
  cin >> N;
  vector<ll> P(N * N);
  ll half1;
  ll half2;
  half1 = (ll)(N / 2) - 1;
  half2 = half1 + 1;
  for (ll i = 0; i < N * N; ++i) {
    cin >> P[i];
    P[i]--;
  }
  for (ll i = 0; i < half1 + 1; ++i) {
    for (ll j = 0; j < half1 + 1; ++j) {
      DIST[getId(j, i, N)] = min(i, j);
    }
    for (ll j = half2; j < N; ++j) {
      DIST[getId(j, i, N)] = min(i, N - j - 1);
    }
  }
  for (ll i = half2; i < N; ++i) {
    for (ll j = 0; j < half1 + 1; ++j) {
      DIST[getId(j, i, N)] = min(N - i - 1, j);
    }
    for (ll j = half2; j < N; ++j) {
      DIST[getId(j, i, N)] = min(N - i - 1, N - j - 1);
    }
  }
  for (ll i = 0; i < N * N; ++i) {
    ans += DIST[P[i]];
    change(P[i]);
  }
  cout << ans << endl;
  return 0;
}
