#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int b[n][n], p, q, r, s, x1;
  vector<tuple<int, int, int> > a;
  long int sum = 0;
  for (int i = 0; i < (n * n); i++) {
    cin >> x1;
    b[(x1 - 1) / n][(x1 - 1) % n] = i + 1;
    a.push_back(make_tuple(i + 1, ((x1 - 1) / n), ((x1 - 1) % n)));
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < (n * n); i++) {
    p = 0;
    q = 0;
    r = 0;
    s = 0;
    for (int j = 0; j < get<1>(a[i]); j++) {
      if (b[j][get<2>(a[i])] != 0) p++;
    }
    for (int j = (get<1>(a[i]) + 1); j < n; j++) {
      if (b[j][get<2>(a[i])] != 0) q++;
    }
    for (int j = 0; j < get<2>(a[i]); j++) {
      if (b[get<1>(a[i])][j] != 0) r++;
    }
    for (int j = (get<2>(a[i]) + 1); j < n; j++) {
      if (b[get<1>(a[i])][j] != 0) s++;
    }
    r = min(r, s);
    p = min(p, q);
    r = min(r, p);
    sum = sum + r;
    b[get<1>(a[i])][get<2>(a[i])] = 0;
  }
  cout << sum;
}
