#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

vec idy={1,0,-1,0};
vec idx={0,1,0,-1};

const ll N=500;
mat c(N,vec(N));
mat al(N,vec(N,1));
ll n;

void dfs(ll y, ll x, ll v) {
  for(ll i=0;i<4;i++) {
    ll ny=y+idy[i];
    ll nx=x+idx[i];
    c[y][x]=v;
    if(ny>-1&&nx>-1&&ny<n&&nx<n&&v+al[y][x]<c[ny][nx]) {
      dfs(ny,nx,v+al[y][x]);
    }
  }
}

int main() {
  cin >> n;
  vec p(n*n);
  for(ll i=0;i<n*n;i++) {
    ll y=i/n;
    ll x=i%n;
    c[y][x]=min({y,x,n-1-y,n-1-x});
  }
  for(ll i=0;i<n*n;i++) {
    cin >> p[i];
    p[i]--;
  }
  ll ans=0;
  for(ll i=0;i<n*n;i++) {
    ll y=p[i]/n;
    ll x=p[i]%n;
    ans+=c[y][x];
    al[y][x]=0;
    dfs(y,x,c[y][x]);
  }
  cout << ans << endl;
}