#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int main() {
  int N, P;
  scanf("%d", &N);
  int dist[N + 2][N + 2], ans = 0;
  for (int i = 0; i < N + 2; ++i) {
    for (int j = 0; j < N + 2; ++j)
      dist[i][j] = min(min(i, N + 1 - i), min(j, N + 1 - j));
  }
  pair<int, int> pos[N * N];
  bool emptied[N + 2][N + 2];
  for (int i = 0; i < N + 2; ++i) {
    for (int j = 0; j < N + 2; ++j) emptied[i][j] = true;
  }
  for (int i = 0; i < N * N; ++i) {
    scanf("%d", &P);
    pos[i] = pair<int, int>((P - 1) / N + 1, (P - 1) % N + 1);
  }
  for (int i = 0; i < N * N; ++i) {
    emptied[pos[i].first][pos[i].second] = false;
    for (int j = 0; j < 4; ++j)
      dist[pos[i].first][pos[i].second] =
          min(dist[pos[i].first][pos[i].second],
              dist[pos[i].first + dx[j]][pos[i].second + dy[j]]);
    queue<pair<int, int> > bfs;
    bfs.push(pos[i]);
    while (!bfs.empty()) {
      pair<int, int> x = bfs.front();
      bfs.pop();
      for (int j = 0; j < 4; ++j) {
        if (dist[pos[i].first][pos[i].second] +
                emptied[pos[i].first + dx[j]][pos[i].second + dy[j]] <
            dist[pos[i].first + dx[j]][pos[i].second + dy[j]]) {
          dist[pos[i].first + dx[j]][pos[i].second + dy[j]] =
              dist[pos[i].first][pos[i].second] +
              emptied[pos[i].first + dx[j]][pos[i].second + dy[j]];
          bfs.push(pair<int, int>(pos[i].first + dx[j], pos[i].second + dy[j]));
        }
      }
    }
    ans += dist[pos[i].first][pos[i].second];
  }
  printf("%d", ans);
  return 0;
}
