#include <bits/stdc++.h>
using namespace std;
set<int> ok;
int par[510 * 510];
int rk[510 * 510];
int mi[510 * 510];
pair<int, int> get(int p, int n) {
  int x = (p - 1) % n;
  int y = (p - 1) / n;
  return make_pair(x, y);
}
void init(int n) {
  for (int i = 1; i <= n * n; i++) {
    par[i] = i;
    rk[i] = 0;
    int x = (i - 1) % n;
    int y = (i - 1) / n;
    mi[i] = min(x, n - x - 1);
    mi[i] = min(mi[i], y);
    mi[i] = min(mi[i], n - y - 1);
  }
}
int fd(int x) {
  if (par[x] == x) {
    return x;
  } else {
    int pp = fd(par[x]);
    mi[x] = min(mi[x], mi[pp]);
    mi[pp] = mi[x];
    return par[x] = fd(par[x]);
  }
}
void unite(int x, int y) {
  int mm = min(mi[x], mi[y]);
  x = fd(x);
  y = fd(y);
  if (x == y) {
    mi[x] = mm;
    return;
  }
  if (rk[x] < rk[y]) {
    par[x] = y;
    mi[y] = mm;
  } else {
    par[y] = x;
    mi[x] = mm;
    if (rk[x] == rk[y]) rk[x]++;
  }
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main() {
  int n;
  cin >> n;
  init(n);
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    ok.insert(p);
    int x = (p - 1) % n;
    int y = (p - 1) / n;
    int ad = mi[p];
    for (int j = 0; j < 4; j++) {
      int tx = x + dx[j];
      int ty = y + dy[j];
      if (tx < 0 || tx >= n || ty < 0 || ty >= n) {
        ad = 0;
        mi[p] = -1;
        break;
      }
      int t = ty * n + tx + 1;
      int pp = fd(t);
      ad = min(ad, mi[pp] + 1);
      mi[p] = min(mi[p], mi[pp]);
    }
    for (int j = 0; j < 4; j++) {
      int tx = x + dx[j];
      int ty = y + dy[j];
      if (tx < 0 || tx >= n || ty < 0 || ty >= n) {
        continue;
      }
      int t = ty * n + tx + 1;
      if (ok.count(t)) unite(t, p);
    }
    ans += ad;
  }
  cout << ans << endl;
  return 0;
}
