#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.at(i);
  }
  vector<vector<bool> > emp(n, vector<bool>(n, false));
  int cnt = 0;
  for (int i = 0; i < n * n; i++) {
    int x = (p.at(i) - 1) / n;
    int y = (p.at(i) - 1) % n;
    emp.at(x).at(y) = true;
    int cnta = 0, cntb = 0, cntc = 0, cntd = 0;
    for (int a = 0; a < x; a++) {
      if (!emp.at(a).at(y)) {
        cnta++;
      }
    }
    for (int b = 0; b < y; b++) {
      if (!emp.at(x).at(b)) {
        cntb++;
      }
    }
    cnta = min(cnta, cntb);
    for (int c = x + 1; c < n; c++) {
      if (!emp.at(c).at(y)) {
        cntc++;
      }
    }
    cnta = min(cnta, cntc);
    for (int d = y + 1; d < n; d++) {
      if (!emp.at(x).at(d)) {
        cntd++;
      }
    }
    cnta = min(cnta, cntd);
    cnt += cnta;
  }
  cout << cnt << endl;
}
