#include <bits/stdc++.h>
using namespace std;
int n, ans = 0;
int arr[502][502] = {0};
int brr[502][502] = {0};
int x[] = {0, 1, 0, -1};
int y[] = {1, 0, -1, 0};
int sear(int a, int b) {
  brr[a][b] = 1;
  if (a == 1 || a == n || b == 1 || b == n) return 0;
  int flag = 0;
  int m = min(min(a - 1, b - 1), min(n - a, n - b));
  for (int i = 1; i < a; i++)
    if (!arr[i][b]) flag++;
  m = min(flag, m);
  flag = 0;
  for (int i = a + 1; i <= n; i++)
    if (!arr[i][b]) flag++;
  m = min(flag, m);
  flag == 0;
  for (int i = 1; i < b; i++)
    if (!arr[a][i]) flag++;
  m = min(flag, m);
  flag = 0;
  for (int i = b + 1; i <= n; i++)
    if (!arr[a][i]) flag++;
  m = min(flag, m);
  for (int j = 0; j < 4; j++)
    if (arr[a + x[j]][b + y[j]] && brr[a + x[j]][b + y[j]] == 0) {
      m = min(sear(a + x[j], b + y[j]), m);
    }
  return m;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  int t = n * n;
  while (t--) {
    int m;
    cin >> m;
    int a, b;
    memset(brr, 0, sizeof(brr));
    if (m % n)
      a = m / n + 1, b = m % n;
    else
      a = m / n, b = n;
    if (a == 1 || a == n || b == 1 || b == n) {
      arr[a][b] = 1;
    } else {
      ans += sear(a, b);
      arr[a][b] = 1;
    }
  }
  cout << ans << endl;
}
