#include <bits/stdc++.h>
using namespace std;
using LL = int32_t;
using ULL = int64_t;
using P = pair<LL, LL>;
#define rep(i, n) for (LL i = 0; i < (n); ++i)
#define all(v) (v).begin(), (v).end()
#define DEBUG

LL n; // number of viewers.
vector<LL> inuse;
vector<LL> cost;

bool is_on_edge(LL id) {
  return (id < n) || (id % n == 0) || (id % n == n - 1) || (id >= n * (n - 1));
}

LL update(LL id) {
  inuse.at(id) = 0;

  priority_queue<P, vector<P>, greater<P>> q; // Current cost, node id
  vector<bool> completed(n * n, false);
  
  q.push(make_pair(cost[id], id));
  while(!q.empty()) {
    const long cc = q.top().first; // pivot node cost
    const long ii = q.top().second; // pivot node id
    q.pop();

    //if (completed.at(ii)) continue;
    completed[ii] = true;
    cost[ii] = cc;

    // check four edges
    if (ii >= n && !completed[ii - n]) {
      // has upper
      if (cost[ii - n] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii - n));
      }
    }
    if (ii % n != 0 && !completed[ii - 1]) {
      // has left
      if (cost[ii - 1] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii - 1));
      }
    }
    if (ii % n != n - 1 && !completed[ii + 1]) {
      // has right
      if (cost[ii + 1] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii + 1));
      }
    }
    if (ii < n * (n - 1) && !completed[ii + n]) {
      // has lower
      if (cost[ii + n] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii + n));
      }
    }
  }



  return 0;
}

int main() {
  cin >> n;
  vector<LL> p(n * n);
  rep (i, n * n) {
    cin >> p.at(i);
    p.at(i) -= 1;

    //cout << i << " " << is_on_edge(i) << endl;
  }

  if (n <= 2) {
    cout << 0 << endl;
    return 0;
  }

  inuse.assign(n * n, 1);
  cost.assign(n * n, n * n + 100);
  rep (i, n) {
    rep (j, n) {
      cost.at(i * n + j) = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }

  LL sum = 0;
  rep (i, n * n) {
//    rep(jj, n) {
//      rep(kk, n) {
//        cout << cost.at(jj * n + kk) << "\t";
//      }
//      cout << endl;
//    }
//    cout << endl;
    sum += cost.at(p.at(i));
    update(p.at(i));
  }
  cout << sum << endl;


  return 0;
  #ifdef DEBUG
//  cout << n << endl;
//  rep (i, n * n) {
//    cout << p.at(i) << endl;
//  }
  #endif
}

