#include <bits/stdc++.h>
using namespace std;
int cnt[505][505];
int ix[505 * 505];
int jy[505 * 505];
int arr[505][505];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      arr[i][j]++;
      cnt[i][j] = min({i - 1, j - 1, n - i, n - j});
      int v = (i - 1) * n + j;
      ix[v] = i;
      jy[v] = j;
    }
  }
  long long ans = 0;
  for (int p = 0; p < n * n; p++) {
    int x;
    cin >> x;
    int i = ix[x];
    int j = jy[x];
    arr[i][j]--;
    int ad = 0;
    for (int k = j + 1; k <= n; k++) {
      cnt[i][k] = min(cnt[i][k], cnt[i][j] + ad);
      ad += arr[i][k];
    }
    ad = 0;
    for (int k = j - 1; k >= 1; k--) {
      cnt[i][k] = min(cnt[i][k], cnt[i][j] + ad);
      ad += arr[i][k];
    }
    ad = 0;
    for (int k = i + 1; k <= n; k++) {
      cnt[k][j] = min(cnt[k][j], cnt[i][j] + ad);
      ad += arr[k][j];
    }
    ad = 0;
    for (int k = i - 1; k >= 1; k--) {
      cnt[k][j] = min(cnt[k][j], cnt[i][j] + ad);
      ad += arr[k][j];
    }
    ans += cnt[i][j];
    if (cnt[i][j]) cnt[i][j]--;
  }
  cout << ans << endl;
  return 0;
}
