#include <stdio.h>

int main(){
   int  ban[502][502],i,j,k,l,n,n1,x,y,p,t,tt,to,aru[502][502];
   long ans;

   scanf("%d",&n);
   n1=n+1;i=0;j=n+1;
   for (;i<=j;i++,j--)
      for (k=i;k<=j;k++) ban[k][i]=ban[k][j]=ban[i][k]=ban[j][k]=i;
   for (i=1;i<=n;i++)
      for (j=1;j<=n;j++) aru[i][j]=1;
   for (i=0;i<=n1;i++) aru[i][0]=aru[i][n1]=aru[0][i]=aru[n1][i]=0;
   ans=0;
   for (k=0;k<n*n;k++) {
      scanf("%d",&p);
      p--;
      x=p%n+1;y=p/n+1;
      t=ban[x-1][y];
      if (t>ban[x+1][y]) t=ban[x+1][y];
      if (t>ban[x][y-1]) t=ban[x][y-1];
      if (t>ban[x][y+1]) t=ban[x][y+1];
      ans+=t;
      for (tt=t,l=x;l>0;l--) {
          if (aru[l-1][y]) tt++;
          if (ban[l-1][y]>tt) ban[l-1][y]=tt;
          else                break;
      }
      for (tt=t,l=x;l<n1;l++) {
          if (aru[l+1][y]) tt++;
          if (ban[l+1][y]>tt) ban[l+1][y]=tt;
          else                break;
      }
      for (tt=t,l=y;l>0;l--) {
          if (aru[x][l-1]) tt++;
          if (aru[x][l-1]) ban[x][l-1]=tt;
          else                break;
      }
      for (tt=t,l=y;l<n1;l++) {
          if (aru[x][l+1]) tt++;
          if (ban[x][l+1]>tt) ban[x][l+1]=tt;
          else                break;
      }
      ban[x][y]=t;
      aru[x][y]=0;
/*     printf("p=%d x=%d y=%d ans=%ld\n",p+1,x,y,ans);
   for (i=0;i<=n1;i++) {
      for (j=0;j<=n1;j++) printf("%d ",ban[i][j]);
      puts("");
   }
      puts("");*/
   }
   printf("%ld\n",ans);
   return 0;
}
