#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long N, p;
  vector<long long> P;
  cin >> N;
  for (long long i = 0; i < (N * N); ++i) {
    cin >> p;
    p--;
    P.push_back(p);
  }
  long long Ans = 0;
  long long D[514][514];
  bool F[514][514];
  for (long long i = 0; i < (N); ++i) {
    for (long long j = 0; j < (N); ++j) {
      D[i][j] = min({i, j, N - i - 1, N - j - 1});
      F[i][j] = false;
    }
  }
  long long dx[4] = {1, 0, -1, 0};
  long long dy[4] = {0, 1, 0, -1};
  for (long long _ = 0; _ < (P.size()); ++_) {
    long long i = P[_] / N;
    long long j = P[_] % N;
    Ans += D[i][j];
    F[i][j] = true;
    map<pair<long long, long long>, bool> M;
    queue<pair<long long, long long>> Q;
    Q.push({i, j});
    M[{i, j}] = true;
    while (!Q.empty()) {
      long long u = Q.front().first;
      long long v = Q.front().second;
      Q.pop();
      M[{u, v}] = true;
      for (long long k = 0; k < (4); ++k) {
        long long y = u + dy[k];
        long long x = v + dx[k];
        if (!(0 <= x && x < N)) continue;
        if (!(0 <= y && y < N)) continue;
        if (M[{y, x}]) continue;
        if (D[y][x] <= D[u][v] + 1 - (long long)F[u][v]) continue;
        Q.push({y, x});
        D[y][x] = D[u][v] + 1 - (long long)F[u][v];
      }
    }
  }
  cout << Ans << endl;
  return 0;
}
