#include <bits/stdc++.h>
using namespace std;
int a[505][505], b[505][505];
int pans, ans, ansx;
void fun(int x, int y, int n) {
  if (x == 0 || y == 0 || x > n || y > n) {
    if (ans)
      ans = min(pans, ans);
    else
      ans = pans;
  } else if (!b[x][y]) {
    pans += a[x][y];
    b[x][y] = 1;
    fun(x, y + 1, n);
    fun(x, y - 1, n);
    fun(x + 1, y, n);
    fun(x - 1, y, n);
    pans -= a[x][y];
    b[x][y] = 0;
  }
}
int main() {
  int n, c, l;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  for (int i = 1; i <= n * n; i++) {
    memset(b, 0, sizeof(b));
    cin >> c;
    if (c % n == 0)
      l = n;
    else
      l = c % n;
    a[(c + n - 1) / n][l] = 0;
    pans = 1;
    fun((c + n - 1) / n, l + 1, n);
    fun((c + n - 1) / n, l - 1, n);
    fun((c + n - 1) / n + 1, l, n);
    fun((c + n - 1) / n - 1, l, n);
    ansx += ans - 1, ans = 0;
  }
  cout << ansx;
}
