#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vecint p(n * n, 0);
  for (auto& e : p) {
    cin >> e;
    e--;
  }
  ll ans = 0;
  vec2int isthere(n, vecint(n, 1));
  for (int k = 0; k < (int)n * n; k++) {
    int i = p[k] / n;
    int j = p[k] % n;
    isthere[i][j] = 0;
    ll a, b, c, d;
    a = b = c = d = 0;
    for (int l = 0; l < (int)j; l++) {
      if (isthere[i][l] == 1) a++;
    }
    for (int l = (int)j + 1; l < (int)n; l++) {
      if (isthere[i][l] == 1) b++;
    }
    for (int l = 0; l < (int)i; l++) {
      if (isthere[l][j] == 1) c++;
    }
    for (int l = (int)i + 1; l < (int)n; l++) {
      if (isthere[l][j] == 1) d++;
    }
    ans += min({a, b, c, d});
  }
  cout << ans << endl;
}
