#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) cin >> p[i];
  vector<vector<int> > table(n, vector<int>(n, 1));
  int sum = 0;
  for (int i = 0; i < n * n; i++) {
    int rowi = (p[i] - 1) / n;
    int linei = (p[i] - 1) % n;
    if ((p[i] <= n) || (p[i] >= n * n - n + 1) || (p[i] % n == 0) ||
        (p[i] % n == 1)) {
      table[rowi][linei] = 0;
    } else {
      int sumup = 0;
      int sumright = 0;
      int sumleft = 0;
      int sumdown = 0;
      for (int j = 0; j < rowi; j++) sumup += table[j][linei];
      for (int j = 0; j < linei; j++) sumleft += table[rowi][j];
      for (int j = linei + 1; j < n; j++) sumright += table[rowi][j];
      for (int j = rowi + 1; j < n; j++) sumdown += table[j][linei];
      sum += min(sumup, min(sumright, min(sumleft, sumdown)));
      table[rowi][linei] = 0;
    }
  }
  cout << sum << endl;
}
