#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B>
inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
template <typename T>
struct SegmentTree {
  using value_type = T;
  using const_reference = const value_type &;
  using F = std::function<value_type(const_reference, const_reference)>;
  using size_type = std::size_t;
  SegmentTree(size_type n_, const F &f, const_reference id_elem)
      : f(f), id_elem(id_elem) {
    n = 1;
    while (n < n_) n <<= 1;
    node.resize(2 * n, id_elem);
  }
  size_type size() const noexcept { return n; }
  void set(size_type i, const_reference x) {
    assert(i < size());
    node[i += n] = x;
    while (i > 1) {
      i >>= 1;
      node[i] = f(node[i << 1], node[i << 1 | 1]);
    }
  }
  void add(size_type i, const_reference x) {
    assert(i < size());
    i += n;
    node[i] = f(node[i], x);
    while (i > 1) {
      i >>= 1;
      node[i] = f(node[i << 1], node[i << 1 | 1]);
    }
  }
  value_type fold(size_type l, size_type r) const {
    assert(l <= size() && r <= size());
    std::vector<size_type> front, back;
    value_type lv = id_elem, rv = id_elem;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) lv = f(lv, node[l++]);
      if (r & 1) rv = f(node[r - 1], rv);
    }
    return f(lv, rv);
  }
  size_type lower_bound(const_reference x) const {
    if (node[1] < x) return n;
    size_type idx;
    value_type s = id_elem;
    for (idx = 1; idx < n;) {
      value_type nex = f(s, node[idx << 1]);
      if (nex < x) {
        idx = idx << 1 | 1;
        s = nex;
      } else
        idx = idx << 1;
    }
    return idx - n;
  }
  size_type upper_bound(const_reference x) const {
    if (node[1] <= x) return n;
    size_type idx;
    value_type s = id_elem;
    for (idx = 1; idx < n;) {
      value_type nex = f(s, node[idx << 1]);
      if (nex <= x) {
        idx = idx << 1 | 1;
        s = nex;
      } else
        idx = idx << 1;
    }
    return idx - n;
  }

 private:
  size_type n;
  F f;
  value_type id_elem;
  std::vector<value_type> node;
};
int main() {
  int N;
  cin >> N;
  vector<SegmentTree<int>> row(N,
                               SegmentTree<int>(
                                   N, [](int x, int y) { return x + y; }, 0));
  vector<SegmentTree<int>> col(N,
                               SegmentTree<int>(
                                   N, [](int x, int y) { return x + y; }, 0));
  for (int i = (0); i < (N); ++i)
    for (int j = (0); j < (N); ++j) {
      row[i].add(j, 1);
      col[j].add(i, 1);
    }
  int ans = 0;
  for (int t = (0); t < (N * N); ++t) {
    int P;
    scanf("%d", &P);
    --P;
    int y = P / N;
    int x = P % N;
    int cur = INF;
    chmin(cur, col[x].fold(0, y));
    chmin(cur, col[x].fold(y + 1, N));
    chmin(cur, row[y].fold(0, x));
    chmin(cur, row[y].fold(x + 1, N));
    ans += cur;
    col[x].set(y, 0);
    row[y].set(x, 0);
  }
  cout << ans << endl;
  return 0;
}
