#include <bits/stdc++.h>
using namespace std;
int mini(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int main() {
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
  }
  int output = 0;
  for (int i = 0; i < N * N; i++) {
    int x, y;
    x = (P[i]) % N;
    if (x == 0) {
      x = N;
    }
    y = ((P[i] - 1) / N) + 1;
    int up = 0;
    int down = 0;
    int left = 0;
    int right = 0;
    for (int j = i + 1; j < N * N; j++) {
      int x2, y2;
      x2 = (P[j]) % N;
      if (x2 == 0) {
        x2 = N;
      }
      y2 = ((P[j] - 1) / N) + 1;
      if (x2 == x && y2 < y) {
        up += 1;
      }
      if (x2 < x && y2 == y) {
        left += 1;
      }
      if (x2 > x && y2 == y) {
        right += 1;
      }
      if (x2 == x && y2 > y) {
        down += 1;
      }
    }
    output += min(min(right, left), min(up, down));
  }
  cout << output << endl;
  return 0;
}
