#include <bits/stdc++.h>
using namespace std;
int z[4] = {1, -1, 0, 0}, zz[4] = {0, 0, 1, -1}, q1, q2;
int n, a[510000], b, cd, mp[510][510], q[510][510], ans;
int dfs(int x, int y) {
  for (int xx = 0; xx < 4; xx++) {
    int cx = x + z[xx], cy = y + zz[xx];
    if (mp[cx][cy] > mp[x][y] + q[cx][cy]) {
      mp[cx][cy] > mp[x][y] + q[cx][cy];
      dfs(cx, cy);
    }
  }
  return 0;
}
int main() {
  cin >> n;
  for (int x = 1; x <= n * n; x++) {
    cin >> a[x];
  }
  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= n; y++) {
      q[x][y] = 1;
      mp[x][y] = min(min(x, n - x + 1), min(y, n - y + 1));
    }
  }
  for (int x = 1; x <= n * n; x++) {
    q1 = (a[x] - 1) / n + 1;
    q2 = a[x] % n;
    if (q2 == 0) {
      q2 = n;
    }
    q[q1][q2] = 0;
    ans += mp[q1][q2] - 1;
    mp[q1][q2] -= 1;
    dfs(q1, q2);
  }
  cout << ans << endl;
}
