#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<vector<long long>> x, y;
long long n, p, result;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << std::setprecision(20);
  cin >> n;
  x.assign(n, vector<long long>(0));
  y.assign(n, vector<long long>(0));
  for (long long i = 0; i < n * n; i++) {
    x[i / n].push_back(i);
    y[i % n].push_back(i);
  }
  for (long long i = 0; i < n * n; i++) {
    cin >> p;
    p--;
    auto&& col = x[p / n];
    auto&& row = y[p % n];
    auto&& itrx = lower_bound(col.begin(), col.end(), p);
    if (itrx == col.end()) itrx = col.begin();
    long long ix = distance(col.begin(), itrx);
    auto&& itry = lower_bound(row.begin(), row.end(), p);
    if (itry == row.end()) itry = row.begin();
    long long iy = distance(row.begin(), itry);
    result += min(ix, min(iy, min((long long)col.size() - ix - 1,
                                  (long long)row.size() - iy - 1)));
    col.erase(itrx);
    row.erase(itry);
  }
  cout << result;
}
