#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r, u, d, m;
  node() {
    l = r = u = d = -1;
    m = 0x3f3f3f3f;
  }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n * n);
  vector<node> b(n * n);
  for (int i = 0, x = 0, y = 0; i < n * n; i++, y = (y + 1) % n) {
    cin >> a[i];
    a[i]--;
    b[i].l = (y == 0 ? -1 : i - 1);
    b[i].r = (y == n - 1 ? -1 : i + 1);
    b[i].u = (x == 0 ? -1 : i - n);
    b[i].d = (x == n - 1 ? -1 : i + n);
    b[i].m = min({x, y, n - x - 1, n - y - 1});
    if ((i + 1) % n == 0) {
      x++;
    }
  }
  int ans = 0;
  for (int i : a) {
    ans += b[i].m;
    if (b[i].l != -1) {
      b[b[i].l].r = b[i].r;
      b[b[i].l].m = max(0, min(b[b[i].l].m, b[i].m));
    }
    if (b[i].r != -1) {
      b[b[i].r].l = b[i].l;
      b[b[i].r].m = max(0, min(b[b[i].r].m, b[i].m));
    }
    if (b[i].u != -1) {
      b[b[i].u].d = b[i].d;
      b[b[i].u].m = max(0, min(b[b[i].u].m, b[i].m));
    }
    if (b[i].d != -1) {
      b[b[i].d].u = b[i].u;
      b[b[i].d].m = max(0, min(b[b[i].d].m, b[i].m));
    }
  }
  cout << ans << '\n';
}
