#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T;
  T = 1;
  while (T--) {
    long long n;
    cin >> n;
    vector<int> a(n * n);
    for (int i = 0; i < n * n; i++) {
      cin >> a[i];
    }
    vector<int> vis((n * n) + 1, 0);
    int ans1 = 0;
    for (int j = 0; j < n * n; j++) {
      int i = a[j];
      vis[a[j]] = 1;
      if ((i / n) == 0 || (i == n)) {
        continue;
      }
      if (i / n == (n - 1) || i == n * n) {
        continue;
      }
      if (i % n == 0) {
        continue;
      }
      int k = i / n;
      int l = (i % n) - 1;
      int r = n - (i / n) - 1;
      int q = (n - (i % n)) % n;
      if (l == 0 || q == 0) {
        continue;
      }
      int c = 0;
      int m = i;
      while (k--) {
        if (vis[m - n] == 0) {
          c++;
        }
        m = m - n;
      }
      int ans = c;
      c = 0;
      m = i;
      while (l--) {
        if (vis[m - 1] == 0) {
          c++;
        }
        m--;
      }
      ans = min(ans, c);
      c = 0;
      m = i;
      while (r--) {
        if (vis[m + n] == 0) {
          c++;
        }
        m = m + n;
      }
      ans = min(ans, c);
      c = 0;
      m = i;
      while (q--) {
        if (vis[m + 1] == 0) {
          c++;
        }
        m++;
      }
      ans = min(ans, c);
      ans1 += ans;
    }
    cout << ans1 << endl;
  }
  return 0;
}
