#include <bits/stdc++.h>
using namespace std;
using LL = int32_t;
using ULL = int64_t;
using P = pair<LL, LL>;
LL n;
vector<LL> inuse;
vector<LL> cost;
vector<LL> completed;
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;
  vector<LL> queued;
  q.push(make_pair(cost[id], id));
  while (!q.empty()) {
    const long cc = q.top().first;
    const long ii = q.top().second;
    q.pop();
    completed[ii] = true;
    cost[ii] = cc;
    queued.push_back(ii);
    if (ii >= n && !completed[ii - n]) {
      if (cost[ii - n] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii - n));
      }
    }
    if (ii % n != 0 && !completed[ii - 1]) {
      if (cost[ii - 1] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii - 1));
      }
    }
    if (ii % n != n - 1 && !completed[ii + 1]) {
      if (cost[ii + 1] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii + 1));
      }
    }
    if (ii < n * (n - 1) && !completed[ii + n]) {
      if (cost[ii + n] > cc + inuse[ii]) {
        q.push(make_pair(cc + inuse[ii], ii + n));
      }
    }
  }
  for (const auto e : queued) {
    completed[e] = false;
  }
  return 0;
}
int main() {
  cin >> n;
  vector<LL> p(n * n);
  for (LL i = 0; i < (n * n); ++i) {
    cin >> p.at(i);
    p.at(i) -= 1;
  }
  if (n <= 2) {
    cout << 0 << endl;
    return 0;
  }
  inuse.assign(n * n, 1);
  cost.assign(n * n, n * n + 100);
  completed.assign(n * n, false);
  for (LL i = 0; i < (n); ++i) {
    for (LL j = 0; j < (n); ++j) {
      cost.at(i * n + j) = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }
  LL sum = 0;
  for (LL i = 0; i < (n * n); ++i) {
    sum += cost.at(p.at(i));
    update(p.at(i));
  }
  cout << sum << endl;
  return 0;
}
