#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
using Pa = pair<T, U>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vec<int> P(M);
  vec<Pa<int, int>> A(M);
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
    A[i] = {P[i] / N, P[i] % N};
  }
  int inf = 1e9;
  vec<int> mi(N * N);
  auto id = [&](int x, int y) { return x * N + y; };
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      mi[id(i, j)] = min({i, j, N - i - 1, N - j - 1});
    }
  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };
  auto dist = [&](int a, int b) {
    return abs(A[a].first - A[b].first) + abs(A[a].second - A[b].second);
  };
  ll ans = 0;
  for (int i = 0; i < M; i++) {
    vec<int> d(i + 1, inf), used(i + 1, 0);
    d[i] = 0;
    while (true) {
      int v = -1;
      int now = inf;
      for (int j = 0; j <= i; j++) {
        if (!used[j]) {
          if (v == -1 || now > d[j]) {
            v = j;
            now = d[j];
          }
        }
      }
      if (v == -1) break;
      used[v] = 1;
      for (int j = 0; j <= i; j++) {
        if (v != j) d[j] = min(d[j], d[v] + dist(v, j) - 1);
      }
    }
    int res = inf;
    for (int j = 0; j <= i; j++) res = min(res, d[j] + mi[P[j]]);
    ans += res;
  }
  cout << ans << "\n";
}
