#include <bits/stdc++.h>
using namespace std;
int arr[503][503] = {0};
int dist[503][503] = {0};
pair<int, int> p[503][503];
int N;
void init(int N) {
  for (int i = 0; i <= N + 1; i++) {
    arr[i][0] = arr[i][N + 1] = arr[0][i] = arr[N + 1][i] = 1;
    p[i][0] = p[i][N + 1] = p[0][i] = p[N + 1][i] = make_pair(1, 0);
  }
}
pair<int, int> get_p(int x, int y) {
  if (p[x][y].first == x && p[x][y].second == y)
    return make_pair(x, y);
  else
    return p[x][y] = get_p(p[x][y].first, p[x][y].second);
}
void uni(pair<int, int> a, pair<int, int> b) {
  pair<int, int> rp = get_p(a.first, a.second);
  pair<int, int> rp2 = get_p(b.first, b.second);
  p[rp.first][rp.second] = rp2;
}
int get_tdi(pair<int, int> a) { return dist[a.first][a.second]; }
void set_tdi(pair<int, int> a, int val) { dist[a.first][a.second] = val; }
int get_dist(int x1, int y1, int x2, int y2) {
  int x_dist = x1 - x2;
  int y_dist = y1 - y2;
  if (x_dist < 0) x_dist *= -1;
  if (y_dist < 0) y_dist *= -1;
  return x_dist + y_dist;
}
int pro(const int x, const int y) {
  int min = 1 << 30;
  for (int i = 0; i <= N + 1; i++) {
    for (int j = 0; j <= N + 1; j++) {
      if (p[i][j].first == 0 && p[i][j].second == 0) continue;
      int d = get_dist(x, y, i, j) - 1 + get_tdi(get_p(i, j));
      if (d == -1) {
        puts("!");
      }
      if (min > d) min = d;
    }
  }
  for (int i = 0; i <= N + 1; i++) {
    for (int j = 0; j <= N + 1; j++) {
      if (p[i][j].first == 0 && p[i][j].second == 0) continue;
      int d = min + get_dist(x, y, i, j);
      if (d < get_tdi(get_p(i, j))) {
        set_tdi(get_p(i, j), d);
      }
    }
  }
  dist[x][y] = min;
  p[x][y] = make_pair(x, y);
  const int dx[4] = {-1, 1, 0, 0};
  const int dy[4] = {0, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    if (p[x - dx[i]][y - dy[i]].first || p[x - dx[i]][y - dy[i]].second) {
      pair<int, int> rp = get_p(x - dx[i], y - dy[i]);
      int best_dist = get_tdi(make_pair(x, y));
      if (best_dist > get_tdi(rp)) best_dist = get_tdi(rp);
      uni(make_pair(x, y), make_pair(x - dx[i], y - dy[i]));
      set_tdi(get_p(x, y), best_dist);
    }
  }
  return min;
}
int main() {
  scanf("%d", &N);
  init(N);
  int sum = 0;
  for (int i = 0; i < N * N; i++) {
    int a;
    scanf("%d", &a);
    const int x = (a - 1) % N + 1;
    const int y = (a - 1) / N + 1;
    sum += pro(x, y);
  }
  printf("%d", sum);
  return 0;
}
