/*
        quu..__
         $$$b  `---.__
          "$$b        `--.                          ___.---uuudP
           `$$b           `.__.------.__     __.---'      $$$$"              .
             "$b          -'            `-.-'            $$$"              .'|
               ".                                       d$"             _.'  |
                 `.   /      @Rrrrya                 ..."             .'     |
                   `./                           ..::-'            _.'       |
                    /                         .:::-'            .-'         .'
                   :                          ::''\          _.'            |
                  .' .-.             .-.           `.      .'               |
                  : /'$$|           .@"$\           `.   .'              _.-'
                 .'|$u$$|          |$$,$$|           |  <            _.-'
                 | `:$$:'          :$$$$$:           `.  `.       .-'
                 :                  `"--'             |    `-.     \
                :##.       ==             .###.       `.      `.    `\
                |##:                      :###:        |        >     >
                |#'     `..'`..'          `###'        x:      /     /
                 \                                   xXX|     /    ./
                  \                                xXXX'|    /   ./
                  /`-.                                  `.  /   /
                 :    `-  ...........,                   | /  .'
                 |         ``:::::::'       .            |<    `.
                 |             ```          |           x| \ `.:``.
                 |                         .'    /'   xXX|  `:`M`M':.
                 |    |                    ;    /:' xXXX'|  -'MMMMM:'
                 `.  .'                   :    /:'       |-'MMMM.-'
                  |  |                   .'   /'        .'MMM.-'
                  `'`'                   :  ,'          |MMM<
                    |                     `'            |tbap\
                     \                                  :MM.-'
                      \                 |              .''
                       \.               `.            /
                        /     .:::::::.. :           /
                       |     .:::::::::::`.         /
                       |   .:::------------\       /
                      /   .''               >::'  /
                      `',:                 :    .'
                                           `:.:'   
*/
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false)
int next[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int s[300010];
int vis[550][550];
int jud[550][550];
int cnt = 0;
int minn;
int n;
void dfs(int x, int y, int step)
{   
    jud[x][y] = 1;
    if(x==1||y==1||x==n||y==n){
        minn = min(minn,step);
        return;
    }
    for(int i = 0; i < 4; i++){
        int xx = x+next[i][0];
        int yy = y+next[i][1];
        if(!jud[xx][yy]){
            jud[xx][yy] = 1;
            if(vis[xx][yy]==0){
                dfs(xx,yy,step);
            }else{
                dfs(xx,yy,step+1);
            }
            jud[xx][yy] = 0;
        }
    }   
}
int main()
{
    cin >> n;
    for(int i = 1; i <= n*n; i++) cin >> s[i];
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++) vis[i][j] =(i-1)*n+j;
    }
    for(int i = 1; i <= n*n; i++){
        int x;
        if(s[i]%n==0){
            x = s[i]/n;
        }else{
            x = (s[i]+n)/n;
        }
        int y = s[i]%n;
        if(y==0) y = n;
        if(x==1||y==1||x==n||y==n){
            vis[x][y] = 0;
            continue;
        }
        minn = mod;
        mem(jud);
        dfs(x,y,0);
        vis[x][y] = 0;
        cnt += minn;
    }
    cout << cnt << endl;
    return 0;
}
