#include <bits/stdc++.h>
using namespace std;
long long n, q, a, b, c, dp[2][501][501], A, B, ans, AA[501], BB[501], MIN;
vector<long long> v;
void bitadd(long long kk, long long key, long long a) {
  while (a <= 500) {
    dp[kk][key][a] += 1;
    a += a & (-a);
  }
  return;
}
long long bitsum(long long k, long long key, long long a) {
  long long tot = 0;
  while (a > 0) {
    tot += dp[k][key][a];
    a -= a & (-a);
  }
  return tot;
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> a;
    v.push_back(a);
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < n * n; i++) {
    A = (v[i] + n - 1) / n;
    B = v[i] % n;
    if (B == 0) B = n;
    MIN = min(bitsum(0, A, B), AA[A] - bitsum(0, A, B));
    MIN = min(MIN, min(bitsum(1, B, A), BB[B] - bitsum(1, B, A)));
    AA[A]++;
    BB[B]++;
    bitadd(0, A, B);
    bitadd(1, B, A);
    ans += min(1LL, MIN);
  }
  cout << ans << endl;
}
