#include <bits/stdc++.h>
using namespace std;
int ans;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<int>> v(N + 1, vector<int>(N + 1));
  vector<pair<int, int>> trash;
  for (int i = 1; i <= (N + 1) / 2; i++) {
    for (int j = i; j <= N + 1 - i; j++) {
      if (i == 1) {
        v[1][j] = 0;
        v[N][j] = 0;
        v[j][N - i + 1] = 0;
        v[j][N * (i - 1) + 1] = 0;
      } else {
        v[i][j] =
            min(v[i - 1][j], min(v[i + 1][j], min(v[i][j - 1], v[i][j + 1]))) +
            1;
        v[N - i + 1][j] =
            min(v[N - i + 2][j], min(v[N - i][j], min(v[N - i + 1][j + 1],
                                                      v[N - i + 1][j - 1]))) +
            1;
        v[j][N - i + 1] =
            min(v[j + 1][N - i + 1],
                min(v[j - 1][N - i + 1], min(v[j][N - i + 2], v[j][N - i]))) +
            1;
        v[j][i] =
            min(v[j + 1][i], min(v[j - 1][i], min(v[j][i + 1], v[j][i - 1]))) +
            1;
      }
    }
  }
  for (long long k = 0; k < (long long)(N * N); ++k) {
    int p;
    cin >> p;
    int column = (p % N == 0 ? N : p % N);
    int row = (p % N == 0 ? p / N : p / N + 1);
    for (int i = 1; i <= (N + 1) / 2; i++) {
      for (int j = i; j <= N + 1 - i; j++) {
        if (i == 1) {
          v[1][j] = 0;
          v[N][j] = 0;
          v[j][N - i + 1] = 0;
          v[j][N * (i - 1) + 1] = 0;
        } else {
          v[i][j] = min(v[i - 1][j],
                        min(v[i + 1][j], min(v[i][j - 1], v[i][j + 1]))) +
                    1;
          v[N - i + 1][j] =
              min(v[N - i + 2][j], min(v[N - i][j], min(v[N - i + 1][j + 1],
                                                        v[N - i + 1][j - 1]))) +
              1;
          v[j][N - i + 1] =
              min(v[j + 1][N - i + 1],
                  min(v[j - 1][N - i + 1], min(v[j][N - i + 2], v[j][N - i]))) +
              1;
          v[j][i] = min(v[j + 1][i],
                        min(v[j - 1][i], min(v[j][i + 1], v[j][i - 1]))) +
                    1;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
