#include <bits/stdc++.h>
int seki[503][503] = {0};
int perans = 100000000;
int n;
void solve(int visit[][503], int nowr, int nowc, int count) {
  visit[nowr][nowc] = 1;
  if (nowr <= 1 || nowr >= n || nowc <= 1 || nowc >= n) {
    perans = ((perans) > (count) ? (count) : (perans));
    return;
  }
  if (visit[nowr + 1][nowc] == 0) {
    solve(visit, nowr + 1, nowc, count + (seki[nowr + 1][nowc] == 0));
  }
  if (visit[nowr - 1][nowc] == 0) {
    solve(visit, nowr - 1, nowc, count + (seki[nowr - 1][nowc] == 0));
  }
  if (visit[nowr][nowc + 1] == 0) {
    solve(visit, nowr, nowc + 1, count + (seki[nowr][nowc + 1] == 0));
  }
  if (visit[nowr][nowc - 1] == 0) {
    solve(visit, nowr, nowc - 1, count + (seki[nowr][nowc - 1] == 0));
  }
}
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;
    int visit[503][503] = {0};
    visit[row][column] = 1;
    solve(visit, row, column, 0);
    ans += perans;
    perans = 100000000;
    seki[row][column] = 1;
  }
  printf("%d", ans);
  return 0;
}
