#include <bits/stdc++.h>
using std::max;
using std::min;
int seat[502][502];
int P[502];
char isVisited[502];
void init(int N) {
  int i, j;
  for (j = 0; j < N; j++) {
    for (i = j; i < N - j; i++) {
      seat[j][i] = j;
      seat[i][j] = j;
      seat[N - j - 1][i] = j;
      seat[i][N - j - 1] = j;
    }
  }
}
void dfs(int x, int y, int N, int val) {
  isVisited[x * N + y] = 1;
  int i, dx, dy;
  int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
  for (i = 0; i < 4; i++) {
    dx = dir[i][0];
    dy = dir[i][1];
    if (seat[x + dx][y + dy] < 0 && isVisited[(x + dx) * N + (y + dy)] == 0) {
      dfs(x + dx, y + dy, N, val);
    }
    if (seat[x + dx][y + dy] > val) {
      seat[x + dx][y + dy] = val;
    }
  }
}
void output(int N) {
  int i, j;
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      printf("%4d", seat[i][j]);
    }
    printf("\n");
  }
}
int main(void) {
  int i, N;
  scanf("%d", &N);
  init(N);
  for (i = 0; i < N * N; i++) {
    scanf("%d", P + i);
  }
  int x, y;
  int kirai = 0;
  for (i = 0; i < N * N; i++) {
    x = (P[i] - 1) / N;
    y = (P[i] - 1) % N;
    kirai += seat[x][y];
    seat[x][y] = -seat[x][y];
    dfs(x, y, N, -seat[x][y]);
    memset(isVisited, 0, 502);
  }
  printf("%d\n", kirai);
  return 0;
}
