#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  int x[300005], y[300005];
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    x[i] = p / n;
    y[i] = p % n;
  }
  int d[503][503];
  bool b[503][503];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[i][j] = false;
      d[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    ans += d[x[i]][y[i]];
    b[x[i]][y[i]] = true;
    queue<pair<int, int> > que;
    que.push(pair<int, int>(x[i], y[i]));
    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      int u = p.first, v = p.second;
      for (int j = 0; j < 4; j++) {
        int nu = u + dx[j], nv = v + dy[j];
        if (b[u][v]) {
          if (nu >= 0 && nu < n && nv >= 0 && nv < n && d[nu][nv] > d[u][v]) {
            d[nu][nv] = d[u][v];
            que.push(pair<int, int>(nu, nv));
          }
        } else {
          if (nu >= 0 && nu < n && nv >= 0 && nv < n &&
              d[nu][nv] > d[u][v] + 1) {
            d[nu][nv] = d[u][v];
            que.push(pair<int, int>(nu, nv));
          }
        }
      }
    }
  }
  cout << ans << endl;
}
