#include <bits/stdc++.h>
using namespace std;
long long prime[100001];
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long vpow(long long a, long long b, long long p) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long temp = vpow(a, b / 2, p);
  temp *= temp;
  temp %= p;
  if (b % 2 == 0) return temp;
  temp = temp * a;
  temp %= p;
  return temp;
}
long long inverseMod(long long a, long long b, long long p) {
  if (b == 0) return 1;
  long long temp = vpow(b, p - 2, p);
  long long ans = temp * a;
  ans %= p;
  return ans;
}
void sieve() {
  prime[0] = prime[1] = 1;
  for (long long i = 2; i < 100001; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < 100001; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}
void solve(long long ttt) {
  long long n;
  cin >> n;
  vector<long long> a(n * n);
  for (__typeof((n * n)) i = (0); i < (n * n); i++) cin >> a[i];
  long long res = 0;
  vector<vector<long long> > dp(n, vector<long long>(n, 1));
  for (__typeof((n * n)) i = (0); i < (n * n); i++) {
    long long cur = a[i] - 1;
    a[i]--;
    long long R, C;
    R = a[i] / n;
    C = a[i] % n;
    long long ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
    for (__typeof((R)) r = (0); r < (R); r++) {
      if (dp[r][C]) ans1++;
    }
    for (__typeof((n)) r = (R + 1); r < (n); r++) {
      if (dp[r][C]) ans2++;
    }
    for (__typeof((C)) c = (0); c < (C); c++) {
      if (dp[R][c]) ans3++;
    }
    for (__typeof((n)) c = (C + 1); c < (n); c++) {
      if (dp[R][c]) ans4++;
    }
    dp[R][C] = 0;
    res += min(min(ans1, ans2), min(ans3, ans4));
  }
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cout << fixed << setprecision(12);
  for (__typeof((t + 1)) q = (1); q < (t + 1); q++) {
    solve(q);
  }
  return 0;
}
