#include <bits/stdc++.h>
int main() {
  int a, input[25000], exit[200000] = {}, blank[25000] = {}, num = 1, ans = 0,
                       flag;
  scanf("%d", &a);
  for (int i = 0; i < a * a; i++) {
    scanf("%d", &input[i]);
  }
  for (int i = 0; i < a - 1; i++) {
    exit[i] = i + 1;
    exit[a - 1 + i] = a * (i + 1);
    exit[2 * a - 2 + i] = a * (i + 1) + 1;
    exit[3 * a - 3 + i] = a * a - a + i + 2;
  }
  int k = 4 * a - 4;
  for (int i = 0; i < 25000; i++) {
    flag = 0;
    for (int j = 0; j < k; j++) {
      if (input[i] == exit[j]) flag++;
    }
    if (flag) {
      int n;
      exit[++k] = input[i] - 1;
      exit[++k] = input[i] + 1;
      exit[++k] = input[i] - a;
      exit[++k] = input[i] + a;
      for (int m = 0; m < i; m++) {
        if (input[i] - 1 == blank[m] || input[i] + 1 == blank[m] ||
            input[i] - a == blank[m] || input[i] + a == blank[m]) {
          exit[++k] = blank[m] - 1;
          exit[++k] = blank[m] + 1;
          exit[++k] = blank[m] - a;
          exit[++k] = blank[m] + a;
        }
      }
    } else {
      ans++;
      blank[i] = input[i];
    }
  }
  printf("%d", ans);
  return 0;
}
