#include <bits/stdc++.h>
int seki[503][503] = {0};
int perans = 100000000;
int n;
int visit[503][503] = {0};
void solve(int visit[][503], int nowr, int nowc, int count, int fr, int fc) {
  visit[nowr][nowc] = 1;
  if (nowr <= 1 || nowr >= n || nowc <= 1 || nowc >= n) {
    printf("count : %d\n", count);
    perans = ((perans) > (count) ? (count) : (perans));
    printf("perans : %d\n", perans);
    return;
  }
  int flag = 1;
  if (visit[nowr + 1][nowc] == 0) {
    flag = 0;
    solve(visit, nowr + 1, nowc, count + (seki[nowr + 1][nowc] == 0), fr, fc);
  }
  if (visit[nowr - 1][nowc] == 0) {
    flag = 0;
    solve(visit, nowr - 1, nowc, count + (seki[nowr - 1][nowc] == 0), fr, fc);
  }
  if (visit[nowr][nowc + 1] == 0) {
    flag = 0;
    solve(visit, nowr, nowc + 1, count + (seki[nowr][nowc + 1] == 0), fr, fc);
  }
  if (visit[nowr][nowc - 1] == 0) {
    flag = 0;
    solve(visit, nowr, nowc - 1, count + (seki[nowr][nowc - 1] == 0), fr, fc);
  }
  if (flag) return;
}
int main() {
  int n;
  scanf("%d", &n);
  int jun[2505] = {0};
  for (int i = 1; i <= n * n; i++) scanf("%d", &jun[i]);
  int ans = 0;
  for (int j = 1; j <= n * n; j++) {
    int i = jun[j];
    int row = (i - 1) / n + 1, column = (i - 1) % n + 1;
    printf("row : %d  column : %d\n", row, column);
    for (int ii = 1; ii <= n; ii++)
      for (int jj = 1; jj <= n; jj++) printf("%d ", visit[ii][jj]);
    puts("");
    visit[row][column] = 1;
    solve(visit, row, column, 0, row, column);
    for (int ii = 1; ii <= n; ii++)
      for (int jj = 1; jj <= n; jj++) printf("%d,", visit[ii][jj]);
    puts("");
    ans += perans;
    perans = 100000000;
    seki[row][column] = 1;
  }
  printf("%d\n", ans);
  return 0;
}
