#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, mod = 1e4 + 7;
const double pi = acos(-1);
int N, a[505][505], fl[2505], sum = 0;
int f(int g) {
  int gi, gj;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      if (a[i][j] == g) {
        gi = i;
        gj = j;
      }
    }
  int k = 1e5, mid = 0;
  for (int i = 1; i < gi; i++)
    if (a[i][gj]) mid++;
  k = min(k, mid);
  mid = 0;
  for (int i = gi + 1; k && i <= N; i++)
    if (a[i][gj]) mid++;
  k = min(k, mid);
  mid = 0;
  for (int j = 1; k && j < gj; j++)
    if (a[gi][j]) mid++;
  k = min(k, mid);
  mid = 0;
  for (int j = gj + 1; j <= N && k; j++)
    if (a[gi][j]) mid++;
  k = min(mid, k);
  a[gi][gj] = 0;
  return k;
}
int main() {
  scanf("%d", &N);
  int k = 1;
  for (int i = 1; i <= N * N; i++) scanf("%d", &fl[i]);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) a[i][j] = k++;
  for (int i = 1; i <= N * N; i++) {
    int gg;
    gg = fl[i];
    sum += f(gg);
  }
  printf("%d\n", sum);
  return 0;
}
