#include "template.cpp"


int main() {
	cin.tie(0);	ios_base::sync_with_stdio(false);

  int n; cin >> n;

  VI p(n*n); rep(i, n*n) cin >> p[i];

  auto cost = make_v(n, n, 0);
  rep(r, n) rep(c, n) cost[r][c] = min({r, c, n-r-1, n-c-1}) + 1;
  dump(cost);

  ll res = 0;

  int dr[] = {0,0,-1,1}; // LRUD
  int dc[] = {1,-1,0,0};
  auto inside = [&](int r, int c) { return r >= 0 and r < n and c >= 0 and c < n; };

  auto occupied = make_v(n, n, true);

  rep(i, n*n) {
    dump(p[i]);
    p[i]--;
    int rp = p[i] / n, cp = p[i] % n;
    res += cost[rp][cp]-1;
    occupied[rp][cp] = false;
    cost[rp][cp] = rp == 0 or rp == n-1 or cp == 0 or cp == n-1 ? 0 : INF;
    rep(j, 4) {
      int r2 = rp + dr[j], c2 = cp + dc[j];
      if (inside(r2, c2)) chmin(cost[rp][cp], cost[r2][c2]);
    }
    deque<PII> togo;
    togo.emplace_back(rp, cp);
    while (not togo.empty()) {
      auto [r, c] = togo.front(); togo.pop_front();
      // bool dbg = false;
      // if (p[i] == 8 and r == 2 and c == 1) dbg = true;
      rep(j, 4) {
        int r2 = r + dr[j], c2 = c + dc[j];
        // if (dbg) dump(r, c, cost[r][c], r2, c2, cost[r2][c2], cost[r2][c2]+occupied[r2][c2]);
        if (inside(r2, c2) and chmin(cost[r2][c2], cost[r][c] + occupied[r2][c2])) {
          if (occupied[r2][c2]) togo.emplace_back(r2, c2);
          else togo.emplace_front(r2, c2);
        }
      }
    }
    // dump(cost);
  }
  cout << res << newl;
}