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

//Minimum Macros
using ll = long long;
using ul = unsigned long;
using ull = unsigned long long;
#define rep(_i,_n) for(ll _i = 0; _i < _n; ++_i)
#define rep2(_i,_s,_n) for(ll _i = _s; _i < _n; ++_i)
#define elif else if
//Vector Macros
#define vec vector
#define append(_x) push_back(_x)
#define fullSort(_a) sort(_a.begin(), _a.end())
#define inputVecI(_name, _n) vec<int> _name(_n);rep(_i, _n)cin >> _name[_i];
#define inputVecL(_name, _n) vec<long> _name(_n);rep(_i, _n)cin >> _name[_i];
#define inputVecLL(_name, _n) vec<ll> _name(_n);rep(_i, _n)cin >> _name[_i];
#define inputVecUL(_name, _n) vec<ul> _name(_n);rep(_i, _n)cin >> _name[_i];
#define inputVecULL(_name, _n) vec<ull> _name(_n);rep(_i, _n)cin >> _name[_i];
#define inputVecS(_name, _n) vec<string> _name(_n);rep(_i, _n)cin >> _name[_i];
#define maxIdx(_name) distance(_name.begin(), max_element(_name.begin(), _name.end()))
#define printVec(_vec) cout << "{";rep(i, _vec.size()-1)cout << _vec[i] << ",";cout << _vec[_vec.size()-1];cout << "}\n";
//Graph Macros
using Graph = vec<vec<long>>;
inline vec<long> BFS(Graph _g, long start){
  vec<long> _ans(_g.size(), -1); _ans[start] = 0; queue<long> _que; _que.push(start);
  while(!_que.empty()){
    long _target = _que.front(); _que.pop();
    for(long _towhere : _g[_target]){
      if(_ans[_towhere] == -1){
        _ans[_towhere] = _ans[_target] + 1; _que.push(_towhere);
      }
    }
  }
  return _ans;
}


ll getId(ll x, ll y, ll n){
  return x + y * n;
}

ull ans = 0;
int DIST[500 * 500];
int GONE[500 * 500];

ll N;
void change(ll Pi){
  ll distPi = DIST[Pi];
  if(GONE[Pi]){
    if(Pi >= N){
      if(DIST[Pi - N] > distPi) {
        DIST[Pi - N] = distPi;
        change(Pi - N);
        if(GONE[Pi - N] == 1){
        }
      }
    }
    if(Pi % N != 0){
      if(DIST[Pi - 1] > distPi) {
        DIST[Pi - 1] = distPi;
        change(Pi - 1);
        if(GONE[Pi - 1] == 1){
        }
      }
    }
    if(Pi < N * N - N){
      if(DIST[Pi + N] > distPi) {
        DIST[Pi + N] = distPi;
        change(Pi + N);
        if(GONE[Pi + N] == 1){
        }
      }
    }
    if(Pi % N != N - 1){
      if(DIST[Pi + 1] > distPi) {
        DIST[Pi + 1] = distPi;
        change(Pi + 1);
        if(GONE[Pi + 1] == 1){
        }
      }
    }
  }
  else{ //*********************************************
    if(Pi >= N){
      if(DIST[Pi - N] > distPi + 1) {
        DIST[Pi - N] = distPi + 1;
        change(Pi - N);
        if(GONE[Pi - N] == 1){
        }
      }
    }
    if(Pi % N != 0){
      if(DIST[Pi - 1] > distPi + 1) {
        DIST[Pi - 1] = distPi + 1;
        change(Pi - 1);
        if(GONE[Pi - 1] == 1){
        }
      }
    }
    if(Pi < N * N - N){
      if(DIST[Pi + N] > distPi + 1) {
        DIST[Pi + N] = distPi + 1;
        change(Pi + N);
        if(GONE[Pi + N] == 1){
        }
      }
    }
    if(Pi % N != N - 1){
      if(DIST[Pi + 1] > distPi + 1) {
        DIST[Pi + 1] = distPi + 1;
        change(Pi + 1);
        if(GONE[Pi + 1] == 1){
        }
      }
    }
  }
}

int main()
{
  cin >> N;
  vec <ll> P(N * N);
  ll half1;
  ll half2;
  half1 = (ll)(N / 2) - 1;
  half2 = half1 + 1;

  rep(i, N * N){
    cin >> P[i];
    P[i]--;
  }
  //printVec(P);

  rep(i, half1 + 1){
    rep(j, half1 + 1){
      DIST[getId(j, i, N)] = min(i, j);
    }
    rep2(j, half2, N){
      DIST[getId(j, i, N)] = min(i, N - j - 1);
    }
  }
  rep2(i, half2, N){
    rep(j, half1 + 1){
      DIST[getId(j, i, N)] = min(N - i - 1, j);
    }
    rep2(j, half2, N){
      DIST[getId(j, i, N)] = min(N - i - 1, N - j - 1);
    }
  }
  rep(i, N * N){
    ans += DIST[P[i]];
    GONE[P[i]] = 1;
    change(P[i]);

    /*
    rep(i1, N){
    rep(j1, N){
    cout << DIST[getId(j1, i1, N)] << ",";
  }
  rep(j1, N){
  if(GONE[getId(j1, i1, N)] == 0)cout << "#";
  else cout << " ";
}
cout << endl;
}
cout << ans << endl;
*/
//printVec(DIST);
}
cout << ans << endl;
return 0;
}


Measured memory_bytes: 22080