#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
class binary_indexed_tree {
 private:
  vector<T> tree_;

 public:
  explicit binary_indexed_tree(size_t size) : tree_(size + 1) {}
  template <typename Iter>
  explicit binary_indexed_tree(Iter begin, Iter end)
      : binary_indexed_tree(end - begin) {
    for (auto it = begin; it != end; ++it) add(it - begin, *it);
  }
  void add(size_t index, T x) {
    for (size_t i = index + 1; i < tree_.size(); i += i & (-i)) tree_[i] += x;
  }
  T sum(size_t index) const {
    T sum = 0;
    for (size_t i = index + 1; i > 0; i -= i & (-i)) sum += tree_[i];
    return sum;
  }
  T range_sum(size_t l, size_t r) const {
    return l > 0 ? sum(r - 1) - sum(l - 1) : sum(r - 1);
  }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<binary_indexed_tree<int>> hol(n, binary_indexed_tree<int>(n)),
      ver(n, binary_indexed_tree<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      hol[i].add(j, 1);
      ver[i].add(j, 1);
    }
  }
  ll ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int low = p / n;
    int col = p % n;
    auto cnt =
        min({hol[low].range_sum(0, col), hol[low].range_sum(col + 1, n),
             ver[col].range_sum(0, low), ver[col].range_sum(low + 1, n)});
    ans += cnt;
    hol[low].add(col, -1);
    ver[col].add(low, -1);
  }
  cout << ans << endl;
  return 0;
}
