#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;
bool min_u(int& m, int v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}
struct solve {
  vvi f;
  vvi used;
  int n;
  vi vx, vy;
  solve(int n) : n(n) {
    vx = {0, 1, 0, -1};
    vy = {1, 0, -1, 0};
    used.resize(n + 2);
    for (auto& m : used) m.assign(n + 2, 0);
    f.resize(n + 2);
    for (auto& m : f) m.assign(n + 2, -1);
    for (int y = 1; y <= n; y++) {
      for (int x = 1; x <= n; x++) {
        int v = x - 1;
        min_u(v, y - 1);
        min_u(v, n - x);
        min_u(v, n - y);
        f[y][x] = v;
        used[y][x] = 1;
      }
    }
  }
  int armin(int x, int y) {
    int v = n;
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i];
      int ny = y + vy[i];
      min_u(v, f[ny][nx]);
    }
    return v;
  }
  int remove(int x, int y) {
    int ret = f[y][x];
    used[y][x] = 0;
    f[y][x] = armin(x, y);
    queue<pii> q;
    q.push(make_pair(x, y));
    while (!q.empty()) {
      tie(x, y) = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        int nx = x + vx[i];
        int ny = y + vy[i];
        if (f[ny][nx] < 0) continue;
        if (min_u(f[ny][nx], armin(ny, nx) + used[ny][nx])) {
          q.push(make_pair(nx, ny));
        }
      }
    }
    return ret;
  }
  int operator()(vi& p) {
    int ans = 0;
    for (int t : p) {
      int x = (t - 1) % n + 1;
      int y = (t - 1) / n + 1;
      ans += remove(x, y);
    }
    return ans;
  }
};
int main(void) {
  int n;
  while (scanf("%d", &n) == 1) {
    vi p(n * n);
    for (auto& mp : p) scanf("%d", &mp);
    printf("%d\n", solve(n)(p));
  }
  return 0;
}
