#include <bits/stdc++.h>
inline int read() {
  char c;
  int x;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  return x;
}
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 505;
int n, ans, dis[N][N];
bool used[N][N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = std::min(std::min(i - 1, n - i), std::min(j - 1, n - j));
    }
  }
  for (int i = 0; i < n * n; i++) {
    int p = read();
    int x = (p - 1) / n + 1;
    int y = (p - 1) % n + 1;
    used[x][y] = true;
    std::deque<std::pair<int, int>> que;
    for (que.push_back({x, y}); !que.empty();) {
      x = que.front().first;
      y = que.front().second;
      que.pop_front();
      if (used[x][y]) {
        for (int k = 0; k < 4; k++) {
          int vx = x + dx[k];
          int vy = y + dy[k];
          if (vx < 1 || vy < 1 || vx > n || vy > n) {
            continue;
          }
          if (dis[vx][vy] > dis[x][y]) {
            dis[vx][vy] = dis[x][y];
            que.push_front({vx, vy});
          }
        }
      } else {
        for (int k = 0; k < 4; k++) {
          int vx = x + dx[k];
          int vy = y + dy[k];
          if (vx < 1 || vy < 1 || vx > n || vy > n) {
            continue;
          }
          if (dis[vx][vy] > dis[x][y] + 1) {
            dis[vx][vy] = dis[x][y] + 1;
            que.push_back({vx, vy});
          }
        }
      }
    }
    ans += dis[x][y];
  }
  printf("%d\n", ans);
  return 0;
}
