#include <bits/stdc++.h>
using namespace std;
struct __timestamper {};
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    vector<int> p(n * n);
    for (int& x : p) {
      scanf("%d", &x);
      --x;
    }
    vector<vector<int>> d(n, vector<int>(n));
    vector<vector<int>> c(n, vector<int>(n, 1));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min({i, j, n - i - 1, n - j - 1});
      }
    }
    long long ans = 0;
    queue<pair<int, int>> q;
    for (int id : p) {
      int row = id / n;
      int col = id % n;
      ans += d[row][col];
      c[row][col] = 0;
      q.emplace(row, col);
      while (!q.empty()) {
        auto [row, col] = q.front();
        q.pop();
        auto relax = [&](int row, int col, int dd) {
          if (row < 0 || row >= n || col < 0 || col >= n) {
            return;
          }
          if (d[row][col] <= dd) return;
          d[row][col] = dd;
          q.emplace(row, col);
        };
        relax(row - 1, col, d[row][col] + c[row][col]);
        relax(row - 1, col, d[row][col] + c[row][col]);
        relax(row, col - 1, d[row][col] + c[row][col]);
        relax(row, col + 1, d[row][col] + c[row][col]);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
