#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
const long long l_INF = 101010101000;
const int INF = 1010101010;
int main() {
  long long n;
  cin >> n;
  vector<vector<long long>> k(n, vector<long long>(n));
  vector<long long> p(n * n);
  for (long long i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      k[i][j] = 1;
    }
  }
  long long count = 0;
  for (long long i = 0; i < n * n; i++) {
    long long x = p[i] % n;
    long long y = p[i] / n;
    bool judge_0 = true, judge_1 = true, judge_2 = true, judge_3 = true;
    long long count_0 = 0, count_1 = 0, count_2 = 0, count_3 = 0;
    for (long long j = 0; j < y; j++) {
      if (k[x][j] == 1) {
        judge_0 = false;
        count_0++;
      }
    }
    for (long long j = 0; j < x; j++) {
      if (k[j][y] == 1) {
        judge_1 = false;
        count_1++;
      }
    }
    for (long long j = x + 1; j < n; j++) {
      if (k[j][y] == 1) {
        judge_2 = false;
        count_2++;
      }
    }
    for (long long j = y + 1; j < n; j++) {
      if (k[x][j] == 1) {
        judge_3 = false;
        count_3++;
      }
    }
    if ((!judge_0) && (!judge_1) && (!judge_2) && (!judge_3)) {
      long long ans_0 = min(count_0, count_1);
      long long ans_1 = min(count_2, count_3);
      count += min(ans_0, ans_1);
    }
    k[x][y] -= 1;
  }
  cout << count << endl;
}
