#include <bits/stdc++.h>
unsigned int Seat[503][503];
unsigned int VisitTable[503][503];
unsigned int Order[503 * 503];
int Recur_Result = -1;
void Visit(int Vcnt, int Id, int InitPnt, int y, int x) {
  int VisitID =
      VisitTable[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] / 1000;
  int VisitCnt =
      VisitTable[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] % 1000;
  if (VisitID == Id && VisitCnt <= Vcnt) return;
  VisitTable[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] =
      Id * 1000 + Vcnt;
  if (Recur_Result != -1)
    if (Recur_Result <= Vcnt) return;
  if (Seat[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] == 0) {
    Recur_Result = Vcnt;
    return;
  }
  if (Seat[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] == -1) Vcnt--;
  Visit(Vcnt + 1, Id, InitPnt, y, x + 1);
  Visit(Vcnt + 1, Id, InitPnt, y, x - 1);
  Visit(Vcnt + 1, Id, InitPnt, y + 1, x);
  Visit(Vcnt + 1, Id, InitPnt, y - 1, x);
}
int main() {
  int N;
  scanf("%d", &N);
  for (int n = 0; n < N * N; n++) scanf("%d", &Order[n]);
  int InitPnt = N / 2;
  int s = 1;
  for (int y = 0; y < N; y++)
    for (int x = 0; x < N; x++)
      Seat[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] = s++;
  int Result = 0;
  for (int n = 0; n < N * N; n++) {
    int y = (Order[n] - 1) / N;
    int x = (Order[n] - 1) % N;
    {
      Recur_Result = -1;
      Visit(0, Order[n], InitPnt, y, x);
      Result += Recur_Result - 1;
      Seat[(503 / 2) + y - InitPnt][(503 / 2) + x - InitPnt] = -1;
    }
  }
  printf("%d", Result);
  return 0;
}
