#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = int_fast64_t;
using pll = pair<ll,ll>;
constexpr ll INF = 1LL<<60;
constexpr ll MOD = 1e9+7;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
#if loc||local
void dump(){cerr<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cerr<<h<<", ";dump(forward<Ts>(t)...);}
#else
void dump(){}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){}
#endif
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T> ostream &operator<<(ostream& os,vector<T>const& v){for(auto const& vi:v)os<<vi<<" ";return os;}
template<class T,class U> ostream &operator<<(ostream& os,pair<T,U>const& p){os<<p.first<<","<<p.second;return os;}
template<class T>vector<T> vec(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a, Ts... ts){return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));}

constexpr ll dx[]={1,0,-1,0,1,1,-1,-1};
constexpr ll dy[]={0,1,0,-1,1,-1,1,-1};

signed main(){

  int n;
  cin>>n;
  vector<int> p(n*n);
  cin>>p;

  auto isout = vec<int>(n,n);

  auto bfs = [&](int s){
    int sy = (s-1)/n;
    int sx = (s-1)%n;
    auto seen = vec<int>(n,n);
    using T3 = tuple<int,int,int>;
    deque<T3> que;
    que.emplace_back(sy,sx,0);
    while(!que.empty()){
      auto [y,x,c] = que.front();
      dump(y,x,c);
      que.pop_front();
      if(seen[y][x])continue;
      seen[y][x] = 1;
      rep(i,4){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny==-1||nx==-1||ny==n||nx==n)return c;
        if(!isout[ny][nx])que.emplace_back(ny,nx,c+1);
        else que.emplace_front(ny,nx,c);
      }
    }
  };

  int ans = 0;
  for(auto pi:p){
    auto tmp = bfs(pi);
    ans += tmp;
    dump(pi,tmp);
    isout[(pi-1)/n][(pi-1)%n] = 1;
  }
  cout<<(ans)<<endl;

}