#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long li;
#define chmin(a,b) (a>(b)?a=(b),1:0)
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
  cout << "(" << p.first << "," << p.second << ")";
  return os;
}
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

pair<int,int> dx[]={{1,0},{0,1},{-1,0},{0,-1}};
pair<int,int> operator+(pair<int,int> p1,pair<int,int> p2){
  return {p1.first+p2.first,p1.second+p2.second};
}
int find(int a,vector<int>& uf){
  if(uf[a]<0) return a;
  return uf[a]=find(uf[a],uf);
}

void unite(int a,int b,vector<int>& uf,vector<int>& rp){
  a=find(a,uf); b=find(b,uf);
  if(a==b) return;
  if(uf[a]>uf[b]) swap(a,b);
  uf[a]+=uf[b];
  uf[b]=a;
  rp[a]=min(rp[a],rp[b]);
}

int pos(int x,int y,int n){ return x*n+y; }
pair<int,int> xy(int pos,int n){ return {pos/n,pos%n}; }
int legal(pair<int,int> temp,int n){
  int x=temp.first,y=temp.second;
  return (0<=x and x<n and 0<=y and y<n);
}

int dis(int x,int y,int n){
  return min({x,y,n-1-x,n-1-y});
}

int main(){
  int n; cin >>n;
  int sq=n*n;
  li ans=0;
  vector<int> rp(n*n);
  rep(i,n){
    rep(j,n){
      rp[pos(i,j,n)]=dis(i,j,n);
    }
  }
  if(df){
    print(rp);
  }
  vector<int> seen(n*n,0),uf(n*n,-1);
  
  rep(i,sq){
    int p; cin >>p;p--;
    seen[p]=1;
    pair<int,int> pr=xy(p,n);
    int cnt=rp[p];
    //    if(df)print("see",pr);
    rep(d,4){
      pair<int,int> temp=pr+dx[d];
      if(!legal(temp,n)) continue;
      //      if(df)print("ar:",temp);
      int x=temp.first,y=temp.second;
      
      if(seen[pos(x,y,n)]){
        unite(pos(x,y,n),p,uf,rp);
        cnt=min(cnt,rp[find(pos(x,y,n),uf)]);
      }else{
        cnt=min(cnt,1+rp[pos(x,y,n)]);
      }
    }
    rep(d,4){
      pair<int,int> temp=pr+dx[d];
      if(!legal(temp,n)) continue;
      //      if(df)print("ar:",temp);
      int x=temp.first,y=temp.second;
      
      if(!seen[pos(x,y,n)]){
        if(chmin(rp[pos(x,y,n)],cnt) and df) print(temp,cnt);
      }
    }

    ans+=cnt;
    if(df)print(p,cnt);
  }
  print(ans);
}
