#include <bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
struct Problem {
 public:
  UL G[502][502];
  bool Save[502][502];
  bool S[502][502] = {};
  void DFS(UL x, UL y, UL d = 0) {
    if (G[x][y] <= d && Save[x][y] == S[x][y]) return;
    G[x][y] = d;
    Save[x][y] = S[x][y];
    if (S[x][y]) d++;
    DFS(x - 1, y, d);
    DFS(x + 1, y, d);
    DFS(x, y - 1, d);
    DFS(x, y + 1, d);
  }
  void Solve() {
    UL N;
    scanf("%d", &N);
    for (UL i = 0; i < (N); i++)
      for (UL j = 0; j < (N); j++) S[i + 1][j + 1] = true;
    for (UL i = 0; i < (N + 2); i++)
      for (UL j = 0; j < (N + 2); j++) {
        G[i][j] = 10000;
        Save[i][j] = S[i][j];
      }
    for (UL i = 0; i < (N + 2); i++) {
      G[0][i] = G[N + 1][i] = 0;
      G[i][0] = G[i][N + 1] = 0;
    }
    for (UL i = 0; i < (N); i++) {
      DFS(1, i + 1);
      DFS(N, i + 1);
      DFS(i + 1, 1);
      DFS(i + 1, N);
    }
    UL ans = 0;
    for (UL i = 0; i < (N * N); i++) {
      UL P;
      scanf("%d", &P);
      UL x = (P - 1) % N + 1;
      UL y = (P - 1) / N + 1;
      UL d = G[x][y];
      ans += G[x][y];
      S[x][y] = false;
      DFS(x, y, d);
    }
    printf("%d", ans);
  }
};
int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
