#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int field[501][501];
bool used[501][501];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  queue<pair<int, int> > que;
  for (int i = 0; i < (int)(N); i++)
    for (int j = 0; j < (int)(N); j++) field[i][j] = -1;
  for (int i = 0; i < (int)(N); i++)
    field[i][0] = field[i][N - 1] = 0, que.push({i, 0}), que.push({i, N - 1});
  for (int j = 0; j < (int)(N); j++)
    field[0][j] = field[N - 1][j] = 0, que.push({0, j}), que.push({N - 1, j});
  while (!que.empty()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int i = 0; i < (int)(4); i++) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      if (!(0 <= tx && tx < N && 0 <= ty && ty < N)) continue;
      if (field[ty][tx] != -1) continue;
      field[ty][tx] = field[y][x] + 1;
      que.push({ty, tx});
    }
  }
  int ans = 0;
  for (int i = 0; i < (int)(N * N); i++) {
    int p;
    cin >> p;
    p--;
    int y = p / N;
    int x = p % N;
    ans += field[y][x];
    used[y][x] = true;
    queue<pair<int, int> > que;
    que.push({y, x});
    while (!que.empty()) {
      int y, x;
      tie(y, x) = que.front();
      que.pop();
      for (int i = 0; i < (int)(4); i++) {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if (!(0 <= tx && tx < N && 0 <= ty && ty < N)) continue;
        if (field[ty][tx] <= field[y][x]) continue;
        chmin(field[ty][tx], field[y][x]);
        if (used[ty][tx]) que.push({ty, tx});
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
