#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
T div_floor(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr long long mod = 1e9 + 7;
constexpr long long INF = mod * mod;
constexpr int MAX = 200010;
typedef struct UnionFindTree {
  vector<int> par;
  vector<long long> val;
  UnionFindTree(int n) {
    par.resize(n, -1);
    val.resize(n, 0);
  }
  void set_val(int i, long long x) { val[i] = x; }
  bool is_root(int x) { return par[x] < 0; }
  int find(int x) {
    if (is_root(x)) return x;
    return par[x] = find(par[x]);
  }
  int size(int x) { return -par[find(x)]; }
  long long get_val(int x) { return val[find(x)]; }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (size(x) < size(y)) swap(x, y);
    par[x] += par[y];
    chmin(val[x], val[y]);
    par[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
} UF;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int n;
int idx(int x, int y) { return x * n + y; }
int main() {
  scanf("%d", &n);
  UF uf(n * n);
  bool used[n][n];
  for (int((x)) = (int)(0); ((x)) < (int)((n)); ++((x)))
    for (int((y)) = (int)(0); ((y)) < (int)((n)); ++((y))) {
      int tmp = min({x, n - 1 - x, y, n - 1 - y});
      uf.set_val(idx(x, y), tmp);
      used[x][y] = false;
    }
  long long ans = 0;
  for (int((_)) = (int)(0); ((_)) < (int)((n * n)); ++((_))) {
    int p;
    scanf("%d", &p);
    --p;
    int x = p / n, y = p % n;
    long long tmp = uf.get_val(idx(x, y));
    for (int((i)) = (int)(0); ((i)) < (int)((4)); ++((i))) {
      int nx = x + dx[i], ny = y + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        chmin(tmp, uf.get_val(idx(nx, ny)) + !used[nx][ny]);
      }
    }
    ans += tmp;
    used[x][y] = true;
    for (int((i)) = (int)(0); ((i)) < (int)((4)); ++((i))) {
      int nx = x + dx[i], ny = y + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n && used[nx][ny]) {
        uf.unite(idx(x, y), idx(nx, ny));
      }
    }
  }
  printf("%lld\n", ans);
}
