#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,a[250010],dist[510][510],rem[510][510],ans=0,dx[]={1,-1,0,0},dy[]={0,0,1,-1};

bool out(int x,int y)
{
	return x<1||x>n||y<1||y>n;
}

bool upd(int x,int y)
{
	int sv=dist[x][y];
	rep(i,4)
	{
		int xx=x+dx[i],yy=y+dy[i];
		if(out(xx,yy)) continue;
		dist[x][y]=min(dist[x][y],dist[xx][yy]+rem[xx][yy]);
	}
	return dist[x][y]!=sv;
}

void dfs(int x,int y)//一开始每个位置的举例都在n左右，所有操作完成后都变成0，所以总复杂度O(n^3) 
{
	rep(i,4)
	{
		int xx=x+dx[i],yy=y+dy[i];
		if(out(xx,yy)|| !upd(xx,yy)) continue;
		dfs(xx,yy);
	}
}

int main()
{
	cin>>n;
	rep(i,n*n) scanf("%d",&a[i]);
	repn(i,n) repn(j,n)
	{
		rem[i][j]=1;
		dist[i][j]=min(min(i-1,n-i),min(j-1,n-j));
	}
	rep(i,n*n)
	{
		int x=(a[i]-1)/n+1,y=(a[i]-1)%n+1;
		ans+=dist[x][y];
		rem[x][y]=0;
		dfs(x,y);
	}
	cout<<ans<<endl;
	return 0;
}