#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
const int N = 505;
int dis[N][N];
bool emp[N][N];
int n;
void relax(int si, int sj) {
  priority_queue<pair<int, PII>> heap;
  heap.emplace(dis[si][sj], PII{si, sj});
  while (!heap.empty()) {
    auto [i, j] = heap.top().second;
    heap.pop();
    for (int ii : {i - 1, i, i + 1}) {
      for (int jj : {j - 1, j, j + 1}) {
        if (abs(i - ii) + abs(j - jj) != 1) continue;
        if (ii < 1 || ii > n || jj < 1 || jj > n) continue;
        if (dis[ii][jj] > dis[i][j] + !emp[i][j]) {
          dis[ii][jj] = dis[i][j] + !emp[i][j];
          heap.emplace(-dis[ii][jj], PII{ii, jj});
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min({i - 1, j - 1, n - i, n - j});
    }
  }
  int ans = 0;
  for (int it = 0; it < n * n; it++) {
    int x;
    cin >> x;
    int i = ((x - 1) / n) + 1;
    int j = ((x - 1) % n) + 1;
    ans += dis[i][j];
    emp[i][j] = true;
    relax(i, j);
  }
  cout << ans << endl;
  return 0;
}
