#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int M = 500 + 7;
int x[M], y[M];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int vs[M][M];
int d[M][M];
struct node {
  int x, y;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int z;
    cin >> z;
    x[i] = (z - 1) / n + 1;
    y[i] = (z - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), vs[i][j] = 1;
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += d[x[i]][y[i]];
    vs[x[i]][y[i]] = 0;
    queue<node> q;
    q.push(node{x[i], y[i]});
    while (q.size()) {
      node tp = q.front();
      q.pop();
      int tx = tp.x, ty = tp.y;
      for (int j = 0; j < 4; j++) {
        int px = tx + dx[j], py = ty + dy[j];
        if (px < 1 || px > n || py < 1 || py > n ||
            d[tx][ty] + vs[tx][ty] >= d[px][py])
          continue;
        d[px][py] = d[tx][ty] + vs[tx][ty];
        q.push(node{px, py});
      }
    }
  }
  cout << ans << endl;
  return 0;
}
