#include <bits/stdc++.h>
using namespace std;
bool occupied[505][505];
pair<int, int> pos[250005];
int N;
int distances[505][505];
int leave(int Pi) {
  int currX = pos[Pi].first, currY = pos[Pi].second;
  occupied[currX][currY] = false;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) distances[i][j] = INT_MAX;
  }
  distances[currX][currY] = 0;
  deque<pair<int, int> > squares;
  squares.push_front(make_pair(currX, currY));
  int ans = INT_MAX;
  while (!squares.empty()) {
    int X = squares.front().first, Y = squares.front().second;
    squares.pop_front();
    if (X == 1 || X == N) {
      ans = min(ans, distances[X][Y]);
      continue;
    } else if (Y == 1 || Y == N) {
      ans = min(ans, distances[X][Y]);
      continue;
    }
    for (int deltaX = -1; deltaX <= 1; deltaX++) {
      for (int deltaY = -1; deltaY <= 1; deltaY++) {
        if (deltaX == 0 && deltaY == 0) continue;
        if (deltaX != 0 && deltaY != 0) continue;
        int finalX = X + deltaX, finalY = Y + deltaY;
        if (occupied[finalX][finalY] &&
            distances[X][Y] + 1 < distances[finalX][finalY]) {
          distances[finalX][finalY] = distances[X][Y] + 1;
          squares.push_back(make_pair(finalX, finalY));
        } else if (!occupied[finalX][finalY] &&
                   distances[X][Y] < distances[finalX][finalY]) {
          distances[finalX][finalY] = distances[X][Y];
          squares.push_front(make_pair(finalX, finalY));
        }
      }
    }
  }
  return ans;
}
int main() {
  scanf("%d", &N);
  int currPerson = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      occupied[i][j] = true;
      pos[currPerson] = make_pair(i, j);
      currPerson++;
    }
  }
  int ans = 0;
  for (int i = 1; i <= N * N; i++) {
    int Pi;
    scanf("%d", &Pi);
    ans += leave(Pi);
  }
  printf("%d", ans);
}
