#include <bits/stdc++.h>
using namespace std;
int operation(int a, int b) { return a + b; }
class SegmentTree {
 protected:
  vector<int> data;
  unsigned long size;
  inline unsigned long parent(unsigned long i) { return i >> 1; }
  inline unsigned long lChild(unsigned long i) { return i << 1; }
  inline unsigned long rChild(unsigned long i) { return i << 1 | 1; }
  inline unsigned long sibling(unsigned long i) { return i ^ 1; }
  inline unsigned long element(unsigned long i) { return i + size; }
  inline bool isRoot(unsigned long i) { return i == 1; }
  inline bool islChild(unsigned long i) { return (i & 1) == 0; }
  inline bool isrChild(unsigned long i) { return (i & 1) != 0; }
  int defaultValue;

 public:
  explicit SegmentTree(const vector<int> &list, int defaultTo) {
    size = (1ul << (long)ceil(log2(list.size())));
    data = vector<int>(size * 2, defaultTo);
    defaultValue = defaultTo;
    for (unsigned long i = 0; i < list.size(); i++) data[i + size] = list[i];
    for (unsigned long i = size - 1; i > 0; --i)
      data[i] = operation(data[lChild(i)], data[rChild(i)]);
  }
  void modify(unsigned long position, int value) {
    data[element(position)] = value;
    for (data[position = element(position)]; !isRoot(position);
         position = parent(position)) {
      if (islChild(position))
        data[parent(position)] =
            operation(data[position], data[sibling(position)]);
      if (isrChild(position))
        data[parent(position)] =
            operation(data[sibling(position)], data[position]);
    }
  }
  int query(unsigned long l, unsigned long r) {
    int lAccumulator = defaultValue, rAccumulator = defaultValue;
    for (l = element(l), r = element(r); l < r; l = parent(l), r = parent(r)) {
      if (isrChild(l)) {
        lAccumulator = operation(lAccumulator, data[l++]);
      }
      if (isrChild(r)) {
        rAccumulator = operation(data[--r], rAccumulator);
      }
    }
    return operation(lAccumulator, rAccumulator);
  }
};
void solve() {
  int n;
  cin >> n;
  vector<SegmentTree> rows(n, SegmentTree(vector<int>(n, 1), 0));
  vector<SegmentTree> cols(n, SegmentTree(vector<int>(n, 1), 0));
  long long ans = 0;
  for (int i = 0, lim = n * n; i < lim; i++) {
    int x;
    cin >> x;
    x--;
    int r = x / n, c = x - r * n;
    int least = 0;
    least = min(rows[r].query(0, c), rows[r].query(c + 1, n));
    least = min({least, cols[c].query(0, r), cols[c].query(r + 1, n)});
    ans += least;
    rows[r].modify(c, 0);
    rows[c].modify(r, 0);
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}
