#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
long long a[maxn * maxn], n;
long long val[maxn][maxn];
long long vali[maxn * maxn], valj[maxn * maxn];
bool removed[maxn][maxn];
int main() {
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      long long curr = j + (i - 1) * n;
      val[i][j] = curr;
      vali[curr] = i;
      valj[curr] = j;
    }
  }
  long long result = 0LL;
  for (int i = 1; i <= n * n; i++) {
    long long curr = a[i];
    long long ci = vali[curr];
    long long cj = valj[curr];
    int brl = 0, brr = 0;
    int bru = 0, brd = 0;
    for (int j = 1; j <= n; j++) {
      if (j == cj) continue;
      if (j < cj && !removed[ci][j]) {
        brl++;
      } else if (j > cj && !removed[ci][j]) {
        brr++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (i == ci) continue;
      if (i < ci && !removed[i][cj])
        bru++;
      else if (i > ci && !removed[i][cj])
        brd++;
    }
    result += 1LL * min(min(brl, brr), min(bru, brd));
    removed[ci][cj] = true;
  }
  cout << result << "\n";
}
