#include <bits/stdc++.h>
using namespace std;
int calc(const int P, const int i, const int j, set<int>& row, set<int>& col) {
  int rsmaller = 0, rlarger = 0;
  for (auto v : row) {
    if (v < P) ++rsmaller;
    if (v > P) ++rlarger;
  }
  int csmaller = 0, clarger = 0;
  for (auto v : col) {
    if (v < P) ++csmaller;
    if (v > P) ++clarger;
  }
  return min(min(rsmaller, rlarger), min(csmaller, clarger));
}
int main() {
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int i = 0; i < (int)(N * N); ++i) cin >> P[i];
  vector<set<int>> row(N), col(N);
  for (int i = 0; i < (int)(N); ++i) {
    for (int j = 0; j < (int)(N); ++j) {
      row[i].insert(i * N + j + 1);
      col[j].insert(i * N + j + 1);
    }
  }
  int ans = 0;
  for (int n = 0; n < (int)(N * N); ++n) {
    int i = (P[n] - 1) / N;
    int j = P[n] - N * i - 1;
    ans += calc(P[n], i, j, row[i], col[j]);
    row[i].erase(P[n]);
    col[j].erase(P[n]);
  }
  cout << ans << endl;
  return 0;
}
