#include <bits/stdc++.h>
struct {
  inline operator int() {
    int x;
    return scanf("%d", &x), x;
  }
  inline operator long long() {
    long long x;
    return scanf("%lld", &x), x;
  }
  template <class T>
  inline void operator()(T &x) {
    x = *this;
  }
  template <class T, class... A>
  inline void operator()(T &x, A &...a) {
    x = *this;
    this->operator()(a...);
  }
} read;
const int maxn = 305;
int val[maxn][maxn];
int dis[maxn][maxn];
void update(int x, int y, int d) {
  d += val[x][y];
  if (d >= dis[x][y]) return;
  dis[x][y] = d;
  update(x - 1, y, d);
  update(x + 1, y, d);
  update(x, y - 1, d);
  update(x, y + 1, d);
}
int main() {
  int n = read;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      val[i][j] = 1;
      dis[i][j] = std::min({i, j, n - i + 1, n - j + 1});
    }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int p = read;
    int x = (p - 1) / n + 1, y = (p - 1) % n + 1;
    val[x][y] = 0;
    update(x, y, dis[x][y] - 1);
    ans += dis[x][y];
  }
  printf("%d\n", ans);
}
