#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int arr[3];
} xyc;
int main(void) {
  int N;
  cin >> N;
  int aa[505];
  for (size_t i = 0; i < N * N; i++) {
    cin >> aa[i];
  }
  int mm[N][N];
  for (size_t i = 0; i < (N + 1) / 2; i++) {
    int j = N - i - 1;
    for (size_t x = i; x <= j; x++) {
      mm[i][x] = i;
      mm[j][x] = i;
      mm[x][i] = i;
      mm[x][j] = i;
    }
  }
  bool is_none[N][N];
  for (size_t i = 0; i < N; i++) {
    for (size_t j = 0; j < N; j++) {
      is_none[i][j] = false;
    }
  }
  long long ans = 0;
  for (size_t i = 0; i < N * N; i++) {
    int a = aa[i];
    int x = (a - 1 + N) % N;
    int y = (a - 1) / N;
    ans += mm[y][x];
    is_none[y][x] = true;
    mm[y][x] -= 1;
    queue<xyc> q;
    xyc tmpp = {{x, y, mm[y][x]}};
    q.push(tmpp);
    while (not q.empty()) {
      xyc tmp = q.front();
      q.pop();
      int x0 = tmp.arr[0];
      int y0 = tmp.arr[1];
      int n = tmp.arr[2];
      int xx[4] = {x0, x0, x0 + 1, x0 - 1};
      int yy[4] = {y0 + 1, y0 - 1, y0, y0};
      for (size_t k = 0; k < 4; k++) {
        int xt = xx[k];
        int yt = yy[k];
        if (xt < 0 or N <= xt or yt < 0 or N <= yt) {
          continue;
        }
        if (is_none[yt][xt] == true) {
          if (mm[yt][xt] > n) {
            mm[yt][xt] = n;
            xyc azu = {{xt, yt, mm[yt][xt]}};
            q.push(azu);
          }
        } else {
          if (mm[yt][xt] > n + 1) {
            mm[yt][xt] = n + 1;
            xyc azu = {{xt, yt, mm[yt][xt]}};
            q.push(azu);
          }
        }
      }
    }
  }
  cout << ans << endl;
}
