#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, a, b, c = 0;
  int J = pow(N, 2);
  int p[J];
  cin >> N;
  for (int i = 1; i <= J; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= J; i++) {
    for (int j = 0; j <= N - 1; j++) {
      if ((j * N + 1 <= p[i]) && (p[i] <= (j + 1) * N)) {
        if (i == 1) {
          if (p[1] <= ((j * N + 1 + (j + 1) * N) / 2)) {
            c = p[1] - 1 - j * N;
          } else {
            c = (j + 1) * N - p[1];
          }
          p[1] = 0;
        } else {
          for (int k = 1; k < i; k++) {
            if (p[k] != 0) {
              if (((j * N) + 1 <= p[k]) && (p[k] < p[i])) {
                a = a + 1;
              } else if ((p[i] < p[k]) && (p[k] <= (j + 1) * N)) {
                b = b + 1;
              }
            }
          }
          if (a >= b) {
            c = c + b;
          } else if (a <= b) {
            c = c + a;
          }
          a = 0;
          b = 0;
        }
      }
    }
    p[i] = 0;
  }
  cout << c << endl;
}
