#include <bits/stdc++.h>
using namespace std;
int n;
int count1, count2, count3, count4;
int count5 = 0;
int main() {
  cin >> n;
  vector<int> p(pow(n, 2));
  for (int i = 0; i < pow(n, 2); i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> cos(pow(n, 2), 0);
  for (int i = 0; i < pow(n, 2); i++) {
    count1 = 0;
    count2 = 0;
    count3 = 0;
    count4 = 0;
    cos[p[i]] = -1;
    for (int j = n * (p[i] / n); j <= p[i]; j++) {
      if (cos[j] == 0) count1++;
    }
    for (int j = p[i]; j < n * (1 + p[i] / n); j++) {
      if (cos[j] == 0) count2++;
    }
    for (int j = p[i] % n; j <= p[i]; j = j + n) {
      if (cos[j] == 0) count3++;
    }
    for (int j = p[i]; j < pow(n, 2) + 1; j = j + n) {
      if (cos[j] == 0) count4++;
    }
    count5 += (int)min({count1, count2, count3, count4});
  }
  cout << count5 << endl;
}
