#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<vector<int>> v(N);
  vector<int> x(N);
  int half = N / 2;
  for (int i = 0; i <= N / 2; ++i) {
    x[i] = i;
  }
  int j = 0;
  for (int i = N - 1; i >= N / 2; --i) {
    x[i] = j;
    ++j;
  }
  for (int i = 0; i < N; ++i) {
    v[i] = x;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      v[i][j] = min(v[i][j], v[j][i]);
    }
  }
  int ans = 0, total = N * N, a, b, val, copy, ac, bc;
  for (int i = 0; i < total; ++i) {
    cin >> j;
    --j;
    ac = j / N;
    bc = j % N;
    ans += v[ac][bc];
    val = v[ac][bc];
    a = ac;
    b = bc;
    while (a > 0) {
      --a;
      if (v[a][b] == -1) {
        continue;
      }
      v[a][b] = min(v[a][b], val);
      ++val;
    }
    a = ac;
    b = bc;
    val = v[a][b];
    while (a < N - 1) {
      ++a;
      if (v[a][b] == -1) {
        continue;
      }
      v[a][b] = min(v[a][b], val);
      ++val;
    }
    a = ac;
    b = bc;
    val = v[a][b];
    while (b > 0) {
      --b;
      if (v[a][b] == -1) {
        continue;
      }
      v[a][b] = min(v[a][b], val);
      ++val;
    }
    a = ac;
    b = bc;
    val = v[a][b];
    while (b < N - 1) {
      ++b;
      if (v[a][b] == -1) {
        continue;
      }
      v[a][b] = min(v[a][b], val);
      ++val;
    }
    v[ac][bc] = -1;
  }
  cout << ans << endl;
  return 0;
}
