#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
long long MOD = 1000000007;
struct Edge {
  int src, dst;
  long long weight;
};
int N;
int kyori[505 * 505][4];
int use[505 * 505] = {0};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  for (int(i) = 0; (i) < (int)(N * N); ++(i)) {
    int now = i + 1;
    int dan = (now + N - 1) / N;
    int lef = now % N;
    if (lef == 0) lef += N;
    kyori[now][0] = dan - 1;
    kyori[now][1] = N - lef;
    kyori[now][2] = N - dan;
    kyori[now][3] = lef - 1;
  }
  long long ans = 0;
  for (int(i) = 0; (i) < (int)(N * N); ++(i)) {
    int x;
    cin >> x;
    use[x] = 1;
    int hoge = kyori[x][0];
    for (int(k) = 0; (k) < (int)(4); ++(k)) hoge = min(hoge, kyori[x][k]);
    int dan = (x + N - 1) / N;
    int lef = x % N;
    if (lef == 0) lef += N;
    int temp = hoge;
    for (int k = x - 1; k >= (dan - 1) * N + 1; k--) {
      kyori[k][1] = min(kyori[k][1], temp);
      if (use[k] == 0) temp++;
    }
    for (int k = x + 1; k <= (dan)*N; k++) {
      kyori[k][3] = min(kyori[k][3], temp);
      if (use[k] == 0) temp++;
    }
    for (int kk = dan - 1; kk >= 1; kk--) {
      int k = (kk - 1) * N + lef;
      kyori[k][0] = min(kyori[k][0], temp);
      if (use[k] == 0) temp++;
    }
    for (int kk = dan + 1; kk <= N; kk++) {
      int k = (kk - 1) * N + lef;
      kyori[k][2] = min(kyori[k][2], temp);
      if (use[k] == 0) temp++;
    }
    ans += hoge;
  }
  cout << ans << endl;
}
