#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p.at(i);
  }
  vector<vector<int>> h((n + 2), vector<int>(n + 2));
  for (int i = 0; i < n + 2; ++i) {
    for (int j = 0; j < n + 2; ++j) {
      h.at(i).at(j) = min(min(min(i, n + 1 - i), j), n + 1 - j);
    }
  }
  int ans = 0;
  int d, m, a;
  for (int i = 0; i < n * n; ++i) {
    d = (p.at(i) - 1) / n;
    m = (p.at(i) - 1) % n;
    a = min(min(min(h.at(d).at(m), h.at(d).at(m + 2)), h.at(d + 2).at(m)),
            h.at(d + 2).at(m + 2));
    ans += a;
    h.at(d).at(m) = a;
  }
  printf("%d\n", ans);
  return 0;
}
