#include <bits/stdc++.h>
using namespace std;
void lok() {
  int n;
  cin >> n;
  vector<vector<bool>> ofo(n), amo(n);
  for (auto &o : ofo) {
    o.resize(n);
    o.assign(o.size(), 1);
  }
  for (auto &o : amo) {
    o.resize(n);
    o.assign(o.size(), 1);
  }
  vector<long long> tabo(n * n, 0);
  long long ans = 0;
  for (auto &o : tabo) {
    cin >> o;
    long long hna = 0;
    long long y = (o - 1) / n;
    long long x = (o - 1) % n;
    ofo[y][x] = 0;
    amo[x][y] = 0;
    long long w = accumulate(ofo[y].begin(), ofo[y].begin() + x, 0);
    long long j = accumulate(ofo[y].begin() + x, ofo[y].end(), 0);
    long long l = accumulate(amo[x].begin(), amo[x].begin() + y, 0);
    long long r = accumulate(amo[x].begin() + y, amo[x].end(), 0);
    long long to = min(w, j);
    long long ti = min(l, r);
    hna = min(to, ti);
    ans += hna;
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  lok();
  return 0;
}
