#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long jou(long long N, long long P) {
  if (P == 0) return 1;
  if (P % 2 == 0) {
    long long t = jou(N, P / 2);
    return t * t % 1000000007;
  }
  return (N * jou(N, P - 1)) % 1000000007;
}
struct UF {
  vector<long long> par;
  vector<long long> siz;
  UF(long long sz_) : par(sz_), siz(sz_, 1LL) {
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  void reset(long long sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  long long root(long long x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool unite(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool same(long long x, long long y) { return root(x) == root(y); }
  long long size(long long x) { return siz[root(x)]; }
};
int main() {
  int n;
  cin >> n;
  if (n == 2) {
    cout << 0 << endl;
    return 0;
  }
  vector<vector<int>> p(n, vector<int>(n, 0));
  for (int i = 1; i < (int)((n + 1) / 2); i++) {
    for (int j = i; j <= n - i; j++) {
      p[i][j] = i;
      p[j][i] = i;
      p[n - 1 - i][j] = i;
      p[j][n - 1 - i] = i;
    }
  }
  int ans = 0;
  vector<bool> q(n * n, false);
  for (int i = 0; i < (int)(n * n); i++) {
    int x;
    cin >> x;
    int a = (x - 1) / n;
    int b = (x - 1) % n;
    q[x] = true;
    if (x - n >= 0 && q[x - n]) {
      p[a][b] = min(p[a][b], p[a - 1][b]);
      p[a - 1][b] = min(p[a][b], p[a - 1][b]);
    }
    if (x + n < n * n && q[x + n]) {
      p[a][b] = min(p[a][b], p[a + 1][b]);
      p[a + 1][b] = min(p[a][b], p[a + 1][b]);
    }
    if (x - 1 >= 0 && q[x - 1]) {
      p[a][b] = min(p[a][b], p[a][b - 1]);
      p[a][b - 1] = min(p[a][b], p[a][b - 1]);
    }
    if (x + 1 < n * n && q[x + 1]) {
      p[a][b] = min(p[a][b], p[a][b + 1]);
      p[a][b + 1] = min(p[a][b], p[a][b + 1]);
    }
    ans += p[a][b];
  }
  cout << ans << endl;
}
