/*    特徴    */

/*    教訓    */

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

using ll = long long;
using ld  = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF=1e9+7;
const ll LINF=9223372036854775807;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld EPS = 1e-9; //微調整用（EPSより小さいと0と判定など）

int ii() { int x; if (scanf("%d", &x) == 1) return x; else {cout << "ii() error" << endl; return 0;} }
long long il() { long long x; if (scanf("%lld", &x) == 1) return x; else {cout << "il() error" << endl; return 0;} }
string is() { string x; cin >> x; return x; }
char ic() { char x; cin >> x; return x; }
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v){cerr << #v << endl; for(const auto& xxx: v){cout << xxx.first << " " << xxx.second << "\n";}} //動作未確認
void br() { putchar('\n'); }

#define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(),a.end() //sort(begin_end(vec));
#define REP(i,m,n) for(ll i=(ll)(m) ; i < (ll) (n) ; i++ )
#define rep(i,n) REP(i,0,n)
#define m_p(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define p_b push_back
#define SZ(x) ((int)(x).size) //size()がunsignedなのでエラー避けに

//4近傍（上下左右） rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};

// coutによるpairの出力（空白区切り）
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << " " << p.second << ")";}
// coutによるvectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << " "; //"\t"に変えるとTabで見やすく区切る
  }
  return s;
}
// coutによる多次元vectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

//最大値、最小値の更新。更新したor等しければtrueを返す
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}

// -------- template end - //


// - library ------------- //


// --------- library end - //

ll N;
ll ans;

// 0-indexedでx, yを渡すこと
ll to_idx(ll x, ll y){
  return y * N + x;
}

void to_xy(ll idx, ll &x, ll &y){
  x = idx % N;
  y = idx / N;
}

void dfs(ll cust_idx, vector<ll> &cbuckets, vector<bool> &visited, vector<bool> &occ_buckets){

  ll x, y;
  to_xy(cust_idx, x, y);
  ll next_cost = cbuckets[cust_idx] + occ_buckets[cust_idx];

  rep(ti, 4){
    ll i = dx_4[ti];
    ll j = dy_4[ti];

    ll tmpx = x - i;
    ll tmpy = y - j;
    if (tmpx < 0 || tmpx >= N || tmpy < 0 || tmpy >= N) continue;
    else{
      ll tmpidx = to_idx(tmpx, tmpy);
      if (cbuckets[tmpidx] > next_cost){
        assert(cbuckets[tmpidx] == next_cost + 1);
        cbuckets[tmpidx] = next_cost;
        // 見つけたマスから更にbfs
        dfs(tmpidx, cbuckets, visited, occ_buckets);
      }
    }

  }

}

void exit_xy(ll cust_x, ll cust_y, vector<ll> &cbuckets, vector<bool> &occ_buckets){
//  cout << cust_x << " " << cust_y << "が抜ける" << endl;
  ll cust_idx = to_idx(cust_x, cust_y);
  ll cust_cost = cbuckets[cust_idx];
  ans += cust_cost;

  // 今回のループで探索済みの客番号をvec<bool>で管理
  vector<bool> visited(N*N, false);

  // 今回の客の席を更新
//  cbuckets[cust_idx] = max(0LL, cbuckets[cust_idx] - 1);
  occ_buckets[cust_idx] = false;
  dfs(cust_idx, cbuckets, visited, occ_buckets);

  // rep(i, N*N){
  //   cout << cbuckets[i] << " ";
  //   if (i % N == N - 1) cout << endl;
  // }
}


// - main() -------------- //

int main(){

  N = il();
  ans = 0;

  // マス目の情報を入れるバケツ
  // 0-indexed!!
  vector<ll> cbuckets(N*N, 0);
  vector<bool> occ_buckets(N*N, true);

  if (N%2==0){
    // 偶数の場合
    rep(x, N / 2){
      rep(y, N / 2){
        // 0 ~ y ( y <= N / 2 - 1 ) までの数字を使って埋める
        rep(i, N / 2){
          cbuckets[to_idx(i, y)] = min(y, i);
          cbuckets[to_idx(N-1-i, y)] = min(y, i);
          cbuckets[to_idx(i, N-1-y)] = min(y, i);
          cbuckets[to_idx(N-1-i, N-1-y)] = min(y, i);
        }
      }
    }
  }
  else{
    // 奇数の場合
    rep(x, N / 2){
      rep(y, N / 2){
        // 0 ~ y ( y <= N / 2 - 1 ) までの数字を使って埋める
        rep(i, N / 2){
          cbuckets[to_idx(i, y)] = min(y, i);
          cbuckets[to_idx(N-1-i, y)] = min(y, i);
          cbuckets[to_idx(i, N-1-y)] = min(y, i);
          cbuckets[to_idx(N-1-i, N-1-y)] = min(y, i);
        }
      }
    }
    // 十字に埋める
    rep(xy, N / 2 + 1){
      if (xy == N / 2) cbuckets[to_idx(xy, xy)] = xy;
      else{
        cbuckets[to_idx(xy, N / 2)] = xy;
        cbuckets[to_idx(N-1 - xy, N / 2)] = xy;
        cbuckets[to_idx(N / 2, xy)] = xy;
        cbuckets[to_idx(N / 2, N-1 - xy)] = xy;
      }
    }
  }

  rep(i, N*N){
    ll p = il() - 1; // 観客番号=idx, 0-indexed
    ll cust_x, cust_y; // 今回出る客のx, y座標
    to_xy(p, cust_x, cust_y);

    exit_xy(cust_x, cust_y, cbuckets, occ_buckets);
  }

  cout << ans << endl;



// ---------- main() end - //
}

Measured memory_bytes: 28676