#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;
template <class T>
using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
vi mv_i = {1, -1, 0, 0};
vi mv_j = {0, 0, 1, -1};
signed main() {
  int START_TIME = clock();
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  i32 n;
  cin >> n;
  vec<PII> p;
  for (int_fast32_t i = 0; i < ((int_fast32_t)(n * n)); i++) {
    i32 tmp, h, w;
    cin >> tmp;
    --tmp;
    h = tmp / n;
    w = tmp % n;
    p.emplace_back(h, w);
  }
  vii grid(n, vi(n, 1));
  deque<PII> dq;
  vii dist, init_dist(n, vi(n, inf));
  i64 cnt = 0;
  for (i32 i = 0; i < n * n; ++i) {
    i32 si = p[i].first, sj = p[i].second;
    dist = init_dist;
    dist[si][sj] = 0;
    dq.emplace_back(si, sj);
    grid[si][sj] = 0;
    i32 ui, uj, ni, nj;
    i32 minimum = inf;
    while (!dq.empty()) {
      tie(ui, uj) = dq.front();
      dq.pop_front();
      for (int_fast32_t _ = 0; _ < ((int_fast32_t)(4)); _++) {
        ni = ui + mv_i[_], nj = uj + mv_j[_];
        if (!(0 <= ni && ni < n && 0 <= nj && nj < n)) {
          chmin(minimum, dist[ui][uj]);
        } else {
          bool person = (grid[ni][nj] == 1);
          if (dist[ni][nj] > dist[ui][uj]) {
            dist[ni][nj] = dist[ui][uj] + grid[ni][nj];
            if (person) {
              dq.emplace_back(ni, nj);
            } else {
              dq.push_front({ni, nj});
            }
          }
        }
      }
    }
    cnt += minimum;
  }
  cout << cnt << '\n';
}
