#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  std::vector<long long> a(n * n);
  for (long long i = 0; i < n * n; i++) cin >> a[i];
  vector<vector<long long>> s(n, vector<long long>(n));
  long long k = 1;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      s[i][j] = k++;
    }
  }
  long long ans = 0;
  for (long long i = 0; i < (long long)a.size(); i++) {
    long long row = (a[i] - 1) / n;
    long long col = (a[i] - 1) % n;
    if (row == 0 || row == n - 1 || col == 0 || col == n - 1) {
      s[row][col] = -1;
      continue;
    }
    long long rowb = 0, rowa = 0, colb = 0, cola = 0;
    for (long long i = 0; i < n; i++) {
      if (i < row && s[i][col] != -1) rowb++;
      if (i > row && s[i][col] != -1) rowa++;
      if (i < col && s[row][i] != -1) colb++;
      if (i > col && s[row][i] != -1) cola++;
    }
    ans += min({rowa, rowb, cola, colb});
    s[row][col] = -1;
  }
  cout << ans << "\n";
}
