#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int N, hate1, hate2, hate3, hate4, minh, cnt = 0;
  cin >> N;
  vector<int> p(N * N);
  vector<vector<int>> P(N, vector<int>(N));
  int val = 1;
  vector<vector<int>> taiseki(N, vector<int>(N, val));
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (N); j++) {
      cnt++;
      P[i][j] = cnt;
    }
  }
  for (int i = 0; i < (N * N); i++) {
    cin >> p[i];
    taiseki[p[i] / N][p[i] % N]--;
    for (int j = 0; j < (p[i] / N); j++) {
      if (taiseki[p[i] / N - j][p[i] % N] == 1) {
        hate1++;
      }
    }
    for (int j = 0; j < (N - p[i] / N - 1); j++) {
      if (taiseki[p[i] / N + j][p[i] % N] == 1) {
        hate2++;
      }
    }
    for (int j = 0; j < (p[i] % N - 1); j++) {
      if (taiseki[p[i] / N][p[i] % N - j] == 1) {
        hate3++;
      }
    }
    for (int j = 0; j < (N - p[i] % N); j++) {
      if (taiseki[p[i] / N][p[i] % N + j] == 1) {
        hate4++;
      }
    }
  }
  minh = hate1;
  if (minh > hate2) minh = hate2;
  if (minh > hate3) minh = hate3;
  if (minh > hate4) minh = hate4;
  cout << minh << endl;
  return 0;
}
