#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template <class T> struct LHeap{
  int *hp;
  int *place;
  int size;
  T *val;
  void malloc(int N){
    hp = (int*)std::malloc(N*sizeof(int));
    place=(int*)std::malloc(N*sizeof(int));
    val=(T*)std::malloc(N*sizeof(T));
  }
  void walloc(int N, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&val, N, mem);
  }
  void free(){
    std::free(hp);
    std::free(place);
    std::free(val);
  }
  void init(int N){
    int i;
    size=0;
    for(i=(0);i<(N);i++){
      place[i]=-1;
    }
  }
  void up(int n){
    int m;
    while(n){
      m=(n-1)/2;
      if(val[hp[m]]<=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void down(int n){
    int m;
    for(;;){
      m=2*n+1;
      if(m>=size){
        break;
      }
      if(m+1<size&&val[hp[m]]>val[hp[m+1]]){
        m++;
      }
      if(val[hp[m]]>=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void change(int n, T v){
    T f = val[n];
    val[n] = v;
    if(place[n]==-1){
      place[n] = size;
      hp[size++] = n;
      up(place[n]);
    }
    else{
      if(f < v){
        down(place[n]);
      }
      else if(f > v){
        up(place[n]);
      }
    }
  }
  int pop(void){
    int res = hp[0];
    place[res] = -1;
    size--;
    if(size){
      hp[0]=hp[size];
      place[hp[0]]=0;
      down(0);
    }
    return res;
  }
}
;
struct dimcomp2{
  int B;
  dimcomp2(){
  }
  dimcomp2(int b){
    B = b;
  }
  dimcomp2(int a, int b){
    B = b;
  }
  inline void set(int b){
    B = b;
  }
  inline void set(int a, int b){
    B = b;
  }
  inline int mask(int a, int b){
    return a * B + b;
  }
  inline int operator()(int a, int b){
    return a * B + b;
  }
  inline void para(int mask, int &a, int &b){
    a = mask / B;
    b = mask % B;
  }
  inline void operator()(int mask, int &a, int &b){
    a = mask / B;
    b = mask % B;
  }
}
;
int N;
int P[250000];
int emp[250000];
int main(){
  int WYIGIcGE;
  wmem = memarr;
  int i;
  int j;
  int k;
  int sk;
  int x;
  int y;
  int sx;
  int sy;
  int nc;
  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};
  long long res = 0;
  dimcomp2 dm;
  LHeap<int> hp;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N*N);Lj4PdHRW++){
      rd(P[Lj4PdHRW]);P[Lj4PdHRW] += (-1);
    }
  }
  dm.set(N,N);
  hp.malloc(N*N);
  hp.init(N*N);
  for(x=(0);x<(N);x++){
    for(y=(0);y<(N);y++){
      i = dm(x,y);
      hp.val[i] =min_L(min_L(min_L(x, y), N-1-x), N-1-y);
    }
  }
  for(WYIGIcGE=(0);WYIGIcGE<(N*N);WYIGIcGE++){
    auto &i = P[WYIGIcGE];
    res += hp.val[i];
    emp[i] = 1;
    hp.change(i, hp.val[i]);
    while(hp.size){
      int d;
      k = hp.pop();
      dm(k, x, y);
      for(d=(0);d<(4);d++){
        sx = x + dx[d];
        sy = y + dy[d];
        if(sx < 0 || sy < 0 || sx >= N || sy >= N){
          continue;
        }
        sk = dm(sx, sy);
        nc = hp.val[k] + (1 - emp[k]);
        if(hp.val[sk] > nc){
          hp.change(sk, nc);
        }
      }
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, P[250000];
// int emp[250000];
// 
// {
//   int i, j, k, sk, x, y, sx, sy, nc;
//   int dx[4] = {-1, 1, 0, 0};
//   int dy[4] = {0, 0, -1, 1};
//   ll res = 0;
//   dimcomp2 dm;
//   LHeap<int> hp;
// 
//   rd(N,(P--)(N*N));
//   dm.set(N,N);
//   hp.malloc(N*N);
//   hp.init(N*N);
// 
//   rep(x,N) rep(y,N){
//     i = dm(x,y);
//     hp.val[i] = min(x,y,N-1-x,N-1-y);
//   }
// 
//   rep[P](i,N*N){
//     res += hp.val[i];
//     emp[i] = 1;
// 
//     hp.change(i, hp.val[i]);
//     while(hp.size){
//       k = hp.pop();
//       dm(k, x, y);
//       rep(d,4){
//         sx = x + dx[d];
//         sy = y + dy[d];
//         if(sx < 0 || sy < 0 || sx >= N || sy >= N) continue;
//         sk = dm(sx, sy);
//         nc = hp.val[k] + (1 - emp[k]);
//         if(hp.val[sk] > nc) hp.change(sk, nc);
//       }
//     }
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 8980