#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 3e5+10;
const int maxm = 1e6+10;
const int mod = 998244353;

int n;
int val[505][505],dist[505][505];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

void bfs(int sx,int sy){
    queue<pair<int,int> >q;
    q.push(make_pair(sx,sy));
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;q.pop();
        for(int i=0;i<4;i++){
            int newx=x+dx[i],newy=y+dy[i];
            if(newx<0||newx>n||newy<0||newy>n)continue;
            if(dist[newx][newy]>dist[x][y]+val[x][y]){
                dist[newx][newy]=dist[x][y]+val[x][y];
                q.push(make_pair(newx,newy));
            }
        }
    }
}

int main(){
    cin>>n;
    FOR(i,1,n)FOR(j,1,n)dist[i][j]=min(min(i-1,j-1),min(n-j,n-i)),val[i][j]=1;
    int ans=0;
    FOR(i,1,n)FOR(j,1,n){
        int P;cin>>P;
        int x=(P+n-1)/n;
        int y=P%n==0?n:P%n;
        val[x][y]--;
        bfs(x,y);
        ans+=dist[x][y];
    }
    cout<<ans<<endl;
}
