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

                                    #define FOR(i,a,b) for(int i = (a); i < (b); i++)
                                    #define rep(i,n) FOR(i,0,n)
                                    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
                                    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
                                    #define FILL(a, value) memset(a, value, sizeof(a)

                                    #define SZ(a) (int)a.size()
                                    #define ALL(a) a.begin(), a.end()
                                    #define PB push_back
                                    #define MP make_pair
                                    #define er erase
                                    #define in insert
                                    #define f first
                                    #define s second




                                    typedef  long long LL;
                                    typedef vector<int> VI;
                                    typedef vector<LL > VL;
                                    typedef vector<vector<int > > VVI;
                                    typedef vector<vector<LL>> VVL;
                                    typedef vector <vector<pair<int, int >> >VVPI;
                                    typedef vector<vector<pair<LL, LL >> >VVPL;
                                    typedef vector<pair<LL, LL >> PL;
                                    typedef vector<PL> VPL;
                                    typedef pair<int, int> PII;
                                    typedef pair<LL,LL > PLL ;
                                    typedef vector<PII> VPI;

                                    const double PI = acos(-1.0);
                                    const int INF = 1e9;
                                    const LL LINF = (LL)1e18 * 5;

                                    const double EPS = 1e-7;
                                    const int MAS =  3 *1e5 + 10;
                                    const int mod = 998244353;
                                    const int MOD =1e9+7;
                                    const int LOG= 19;
                                    const int ar = 1e5 +100;
                                    const int MAX = 1e6 + 1e2;
                                    const int root = 0;


                                void min_self(LL &a ,LL b)
                                {
                                    a = min(a,b);
                                }
                                    LL n,a,b,c,d;

                                map<LL,LL > mp;
                                    LL f(LL nd)
                                {
                                    if(nd == 0 )return 0;
                                    if(nd == 1)return d ;
                                    if(mp.count(nd))return mp[nd];
                                    LL best = LINF ;
                                    if(d < LINF / nd)min_self(best,d * nd);
                                    VL vec = { 2,3,5};
                                    VL cost ={a,b,c};
                                    rep(i,3)
                                    {
                                      int mul = vec[i];
                                      LL price = cost[i];
                                      if(nd % mul == 0)
                                      {
                                            min_self(best,price + f(nd  / mul));
                                      }
                                      else
                                      {
                                          LL eq = nd / mul * mul;
                                          min_self(best,price + (nd - eq)* d + f(nd / mul));
                                          LL eq_up = eq + mul;
                                          min_self(best,price + d *(eq_up - nd ) +f(nd/ mul + 1));
                                      }
                                    }
                                    return mp[nd] = best;

                                }
                                const int MAXN = 505;
                               int h[MAXN][MAXN];
                               int oc[MAXN][MAXN];
                               int dx[4] = {0,1,0,-1};
                               int dy[4] = {1,0,-1,0};

                               void dfs(int x ,int y)
                               {
                                //   cout << '*';
                                 int z =h[x][y] + oc[x][y];
                                 rep(side, 4)
                                 {
                                     int x1 = x + dx[side],y1 =y + dy[side];
                                     if(h[x1][y1] > z) {
                                         h[x1][y1] = z;
                                         dfs(x1, y1);
                                     }
                                 }
                               }

                                int main() {
                                    ios_base::sync_with_stdio(false);
                                    cin.tie(NULL);
                                    int n;
                                    cin >> n;
                                    FOR(i,1,n + 1)FOR(j,1,n + 1)
                                    {
                                      h[i][j] = min(min(i,n + 1 - i ),min(j,n + 1 - j )) - 1;
                                      oc[i][j] = 1;
                                    }
                                    LL res =0 ;
                                    rep(i,n* n )
                                    {
                                        int id;cin >> id;
                                        int x =  (id + n -1)/ n;
                                        int y = id % n ;
                                        if(y == 0)y+=n;
                                        res+=h[x][y];
                                        oc[x][y] = 0;
                                        dfs(x,y);
                                    }
                                    cout << res << '\n';
                                     
                                }

















































Measured memory_bytes: 21024