#include <bits/stdc++.h>
using namespace std;
int main() {
  long long ans = 0;
  int N, px, py, ku, kd, kl, kr, minm;
  cin >> N;
  int temp;
  int S[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      S[i][j] = 1;
    }
  }
  for (int i = 0; i < N * N; i++) {
    cin >> temp;
    temp--;
    px = temp / N;
    py = temp % N;
    ku = 0;
    kd = 0;
    kl = 0;
    kr = 0;
    minm = 0;
    for (int d = py + 1; d <= N - 1; d++) {
      if (S[px][d] == 1) {
        kd++;
      }
    }
    for (int u = py - 1; u >= 0; u--) {
      if (S[px][u] == 1) {
        ku++;
      }
    }
    for (int l = px - 1; l >= 0; l--) {
      if (S[l][py] == 1) {
        kl++;
      }
    }
    for (int r = px + 1; r <= N - 1; r++) {
      if (S[r][py] == 1) {
        kr++;
      }
    }
    minm = min(min(min(ku, kd), kr), kl);
    ans += minm;
    S[px][py] = 0;
  }
  cout << ans << endl;
  return 0;
}
