#include <bits/stdc++.h>
using namespace std;
int g[510 * 510];
int n;
void solve() {
  long long ans = 0;
  vector<set<int>> rows(n + 1), cols(n + 1);
  for (int i = 1; i <= n * n; i++) cin >> g[i];
  for (int i = 1; i <= n * n; i++) {
    int l = ceil((long double)g[i] / (long double)n), r = g[i] % n;
    if (!r) r = n;
    if (rows[l].find(r) != rows[l].end()) continue;
    int d1 = std::distance(rows[l].begin(), rows[l].lower_bound(r)), d2, d3, d4,
        x1, y1, x2, y2;
    d2 = std::distance(rows[l].begin(), rows[l].upper_bound(n));
    d3 = std::distance(cols[r].begin(), cols[r].lower_bound(l));
    d4 = std::distance(cols[r].begin(), cols[r].upper_bound(n));
    x1 = (l - 1 - d3);
    y1 = (n - l - (d4 - d3));
    x2 = (r - 1 - d1);
    y2 = (n - r - (d2 - d1));
    if (min(x1, y1) > min(x2, y2))
      ans += min(x2, y2);
    else
      ans += min(x1, y1);
    rows[l].insert(r);
    cols[r].insert(l);
  }
  cout << ans << "\n";
}
int main() {
  cin >> n;
  solve();
}
