#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, ans = 0;
  cin >> n;
  vector<int> p(n * n + 10), v(n * n + 10, -1);
  for (int i = 1; i <= n * n; i++) cin >> p[i];
  for (int i = 1; i <= n * n; i++) {
    int x = p[i] / n, y = p[i] % n;
    if (!(x == 0 || x == n - 1 || y == 0 || y == 1)) {
      int left = y - 1, right = n - y, up = x, down = n - 1 - x;
      for (int j = 1; j < y; j++) {
        if (v[n * x + j] == 1) left--;
      }
      for (int j = y + 1; j <= n; j++) {
        if (v[n * x + j] == 1) right--;
      }
      for (int j = 0; j < x; j++) {
        if (v[n * j + y] == 1) up--;
      }
      for (int j = x + 1; j <= n - 1; j++) {
        if (v[n * j + y] == 1) down--;
      }
      ans += min(min(left, right), min(up, down));
    }
    v[p[i]] = 1;
  }
  cout << ans << endl;
}
