#include <stdio.h>
#define max(x, y) ((x)>(y) ? (y) : (x))

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;
  //  printf("%d  %d", nowr, nowc);
  if(nowr<=1 || nowr>=n || nowc<=1 || nowc>=n){
    perans = max(perans, count);
    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;
    //    printf("jun : %d  row : %d   column : %d\n", jun[j], row, column);
    /*
    if(row==n || in*(n-1) || (i-1)%n==0 || i%n==0){
      seki[row][column] = 1;
      continue;
    }
    */
    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;
}
