#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct BIT {
 private:
  vector<T> data;
  const int n;

 public:
  BIT(int sz) : data(sz + 1, 0), n(sz) {}
  BIT(vector<T> t) : data(t.size() + 1), n(t.size()) {
    for (int i = 0; i < n; ++i) add(i, t[i]);
  }
  T sum(size_t i) {
    T s = 0;
    while (i > 0) {
      s += data[i];
      i -= i & (-i);
    }
    return s;
  }
  T sum(size_t i, size_t j) { return sum(j) - sum(i); }
  void add(int i, T x) {
    ++i;
    assert(i > 0);
    while (i <= n) {
      data[i] += x;
      i += i & (-i);
    }
  }
};
int main() {
  int N;
  cin >> N;
  using bit = BIT<int>;
  vector<int> ini(N, 1);
  vector<bit> H(N, bit(ini)), W(N, bit(ini));
  int ans = 0;
  for (int i = 0; i < N * N; ++i) {
    int p;
    cin >> p;
    --p;
    int h = p / N, w = p % N;
    int d = N;
    H[h].add(w, -1);
    W[w].add(h, -1);
    d = min({d, H[h].sum(0, w), H[h].sum(w + 1, N)});
    d = min({d, W[w].sum(0, h), W[w].sum(h + 1, N)});
    ans += d;
  }
  cout << ans << endl;
}
