#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<vector<int>> v(n, vector<int>(n));
  int a[n][n];
  bool b[n][n];
  for (int i = 0; i < n; i++) {
    int p = i * n;
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      b[i][j] = false;
      v[i][j] = j + p + 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int p = a[i][j];
      int l, m;
      l = p % n == 0 ? p / n - 1 : p / n;
      m = (p - 1) % n;
      b[l][m] = true;
      int q = 0, r = 0, s = 0, t = 0;
      for (int g = m - 1; g >= 0; g--) {
        if (!b[l][g]) q++;
      }
      for (int g = m + 1; g < n; g++) {
        if (!b[l][g]) r++;
      }
      for (int g = l + 1; g < n; g++) {
        if (!b[g][m]) s++;
      }
      for (int g = l - 1; g >= 0; g--) {
        if (!b[g][m]) t++;
      }
      ans += min({q, r, s, t});
    }
  }
  cout << ans << endl;
}
