#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] - 1) / n, y = (p[i] - 1) % n;
    if (!(x == 0 || x == n - 1 || y == 0 || y == n - 1)) {
      int left = y, right = n - 1 - y, up = x, down = n - 1 - x;
      for (int j = 0; j <= n - 1; j++) {
        if (v[n * x + j] == 1 && j < y)
          left--;
        else if (v[n * x + j] == 1 && j > y)
          right--;
      }
      for (int j = 0; j <= n - 1; j++) {
        if (v[n * j + y] == 1 && j < x)
          up--;
        else if (v[n * j + y] == 1 && j > x)
          down--;
      }
      ans += min(min(left, right), min(up, down));
    }
    v[p[i] - 1] = 1;
  }
  cout << ans << endl;
}
