#include <bits/stdc++.h>
using namespace std;
template <class T>
T maxAny(T a, T b) {
  if (a > b)
    return a;
  else
    return b;
}
template <class T>
T minAny(T a, T b) {
  if (a > b)
    return b;
  else
    return a;
}
int main() {
  long long n, ans = 0;
  cin >> n;
  vector<int> lst(n * n + 1);
  for (int i = 0; i < (n * n); i++) cin >> lst[i];
  vector<int> mae(n * n + 1, 0);
  vector<int> usiro(n * n + 1, 0);
  vector<int> migi(n * n + 1, 0);
  vector<int> hidari(n * n + 1, 0);
  for (int i = 1; i <= (n * n); i++) {
    mae[i] = (i - 1) / n;
    usiro[i] = (n - 1) - (i - 1) / n;
    migi[i] = (n - 1) - (i - 1) % n;
    hidari[i] = (i - 1) % n;
  }
  for (int i = 0; i < (n * n); i++) {
    ans +=
        min(min(mae[lst[i]], usiro[lst[i]]), min(migi[lst[i]], hidari[lst[i]]));
    for (int j = 1; lst[i] - n * j > 0; j++) {
      usiro[lst[i] - n * j] -= 1;
    }
    for (int j = 1; lst[i] + n * j <= n * n; j++) {
      mae[lst[i] + n * j] -= 1;
    }
    for (int j = 1; (lst[i] - j) % n != 0; j++) {
      migi[lst[i] - j] -= 1;
    }
    for (int j = 1; (lst[i] + j) % n != 1; j++) {
      hidari[lst[i] + j] -= 1;
    }
  }
  cout << ans << endl;
}
