#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<Z A,Z...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

int b[300001],r[300001],c[300001];
void init(){rep(i,300001)b[i]=i,r[i]=0,c[i]=1;}
int find(int x){return (b[x]==x)?x:(b[x]=find(b[x]));}
void unite(int x,int y) {
  x=find(x),y=find(y);
  if(x==y)return;
  int cx=c[x],cy=c[y];
  if(r[x]<r[y])b[x]=y;
  else{b[y]=x;if(r[x]==r[y])r[x]++;}
  c[find(x)]=min(cx,cy);
}
bool same(int x,int y){return find(x)==find(y);}

ll a[555][555],n;
ll solve(ll sx,ll sy) {
  ll d[n][n];
  rep(i,n)rep(j,n)d[i][j]=MAX;
  deque<P> que;
  que.push_back(P(sx,sy));
  d[sx][sy]=0;
  /*
  rep(i,n-1) {
    d[0][i]=0;
    que.push_back(P(0,i));
    d[n-1][n-1-i]=0;
    que.push_back(P(n-1,n-1-i));
    d[i][n-1]=0;
    que.push_back(P(i,n-1));
    d[n-1-i][0]=0;
    que.push_back(P(n-1-i,0));
  }
  */
  while(!que.empty()) {
    P p=que.front();que.pop_front();
    int x=p.F,y=p.S;
    rep(i,4) {
      int xx=x+dx[i],yy=y+dy[i];
      if(check(n,n,xx,yy)) {
        ll cc=d[x][y];
        if(a[xx][yy]) cc++;
        if(d[xx][yy]>cc) {
          d[xx][yy]=cc;
          if(a[xx][yy]) que.push_back(P(xx,yy));
          else que.push_front(P(xx,yy));
        }
      } else return d[x][y];
    }
    /*
    if(P(x,y)==P(sx,sy)) return d[sx][sy];
    rep(i,4) {
      int xx=x+dx[i],yy=y+dy[i];
      
      if(check(n,n,xx,yy)&&d[xx][yy]==-1) {
        if(a[x][y]) {
          d[xx][yy]=d[x][y]+1;
          que.push_back(P(xx,yy));
        } else {
          d[xx][yy]=d[x][y];
          que.push_front(P(xx,yy));
        }
      }
      
      if(check(n,n,xx,yy)) {
        ll cc=d[x][y];
        if(a[x][y]) cc++;
        if(d[xx][yy]>cc) {
          d[xx][yy]=cc;
          if(a[x][y]) que.push_back(P(xx,yy));
          else que.push_front(P(xx,yy));
        }
      }
    }
    */
  }
  return -1;
}

ll p[255555],m;

void solve1() {
  ll ans=0;
  rep(i,n)rep(j,n)a[i][j]=1;
  rep(i,m) {
    ll x=p[i]/n,y=p[i]%n;
    //a[x][y]=2;
    ll z=solve(x,y);
    ans+=z;
    a[x][y]=0;
  }
  pr(ans);
}

void Main() {
  //srand((unsigned)time(NULL));
  //init();
  R n;
  m=n*n;
  /*
  rep(i,m) p[i]=i;
  random_shuffle(p,p+m);
  */
  
  rep(i,m) R p[i];
  rep(i,m) p[i]--;
  
  //solve1();

  rep(i,n)rep(j,n)a[i][j]=1;
  ll d[n][n];
  memset(d,-1,sizeof(d));
  queue<P> que;
  rep(i,n-1) {
    d[0][i]=0;
    que.push(P(0,i));
    d[n-1][n-1-i]=0;
    que.push(P(n-1,n-1-i));
    d[i][n-1]=0;
    que.push(P(i,n-1));
    d[n-1-i][0]=0;
    que.push(P(n-1-i,0));
  }
  while(!que.empty()) {
    P q=que.front();que.pop();
    int x=q.F,y=q.S;
    rep(i,4) {
      int xx=x+dx[i],yy=y+dy[i];
      if(check(n,n,xx,yy)&&d[xx][yy]==-1) {
        d[xx][yy]=d[x][y]+1;
        que.push(P(xx,yy));
      }
    }
  }
  rep(i,n)rep(j,n)c[i*n+j]=d[i][j];
  ll ans=0;
  rep(k,m) {
    ll x=p[k]/n,y=p[k]%n;
    /*
    ans+=c[p[i]];
    a[x][y]=0;
    rep(j,4) {
      ll xx=x+dx[i],yy=y+dy[i];
      ll z=xx*n+yy;
      if(check(n,n,xx,yy)&&!a[xx][yy]) unite(p[i],z);
    }
    */
    ans+=d[x][y];
    a[x][y]=0;
    rep(i,4) {
      ll xx=x+dx[i],yy=y+dy[i];
      ll z=xx*n+yy;
      if(check(n,n,xx,yy)&&!a[xx][yy]) d[x][y]=min(d[x][y],d[xx][yy]);
    }
    deque<P> que;
    que.push_back(P(x,y));
    while(!que.empty()) {
      P q=que.front();que.pop_front();
      ll x2=q.F,y2=q.S;
      rep(i,4) {
        ll xx=x2+dx[i],yy=y2+dy[i];
        if(check(n,n,xx,yy)) {
          ll cc=d[x2][y2];
          if(a[x2][y2]) cc++;
          if(d[xx][yy]>cc) {
            d[xx][yy]=cc;
            if(a[x2][y2]) que.push_back(P(xx,yy));
            else que.push_front(P(xx,yy));
          }
        }
      }
    }
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


Measured memory_bytes: 12564