#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d", &n);
  int table[502][502] = {0};
  for (int i = 1; i <= (n + 1) / 2; i++) {
    int end = n - i + 1;
    for (int j = i; j <= end; j++) {
      table[i][j] = table[end][j] = i - 1;
      table[j][i] = table[j][end] = i - 1;
    }
  }
  int jun[250005] = {0};
  for (int i = 1; i <= n * n; i++) scanf("%d", jun + i);
  long ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int row = (jun[i] - 1) / n + 1, column = (jun[i] - 1) % n + 1;
    ans += table[row][column];
    int visited[502][502] = {0};
    int queue[25005][2] = {0}, s = 0, e = 0;
    for (int i = -1; i <= 1; i += 2) {
      if (row + i <= n && row + i > 0 && table[row + i][column] != 0) {
        queue[e][0] = row + i;
        queue[e][1] = column;
        e++;
      }
      if (column + i <= n && column + i > 0 && table[row][column + i] != 0) {
        queue[e][0] = row;
        queue[e][1] = column + i;
        e++;
      }
    }
    while (s != e) {
      int nowr = queue[s][0];
      int nowc = queue[s][1];
      s++;
      visited[nowr][nowc] = 1;
      if (table[nowr][nowc] == -1) {
        for (int i = -1; i <= 1; i += 2) {
          if (nowr + i <= n && nowr + i > 0 && table[nowr + i][nowc] != 0 &&
              visited[nowr + i][nowc] == 0) {
            queue[e][0] = nowr + i;
            queue[e][1] = nowc;
            e++;
          }
          if (nowc + i <= n && nowc + i > 0 && table[nowr][nowc + i] != 0 &&
              visited[nowr][nowc + i] == 0) {
            queue[e][0] = nowr;
            queue[e][1] = nowc + i;
            e++;
          }
        }
      } else if (table[nowr][nowc] != 0) {
        table[nowr][nowc] =
            ((table[nowr][nowc]) > (table[row][column]) ? (table[row][column])
                                                        : (table[nowr][nowc]));
      }
    }
    table[row][column] = -1;
  }
  printf("%ld\n", ans);
  return 0;
}
