#include <bits/stdc++.h>
template <class T>
class SegmentTree {
 public:
  SegmentTree(const int n, const T id, std::function<T(T, T)> op,
              std::function<T(T, T)> updateOp)
      : id(id), op(op), updateOp(updateOp) {
    leafNum = 1;
    while (leafNum < n) leafNum *= 2;
    data = std::vector<T>(2 * leafNum - 1, id);
  }
  void update(const int index, const T value) {
    int i = leaf(index);
    data[i] = updateOp(data[i], value);
    while (i > 0) {
      i = parent(i);
      data[i] = op(data[rightChildIndex(i)], data[leftChildIndex(i)]);
    }
  }
  T find(const int l, const int r) { return query(l, r, 0, 0, leafNum); }

 private:
  int leftChildIndex(const int parent) { return parent * 2 + 1; }
  int rightChildIndex(const int parent) { return parent * 2 + 2; }
  int parent(const int child) { return (child - 1) / 2; }
  int leaf(const int index) { return index + leafNum - 1; }
  T query(const int l, const int r, const int node, const int al,
          const int ar) {
    if (r <= al || ar <= l) return id;
    if (l <= al && ar <= r) return data[node];
    int mid = (al + ar) / 2;
    T leftValue = query(l, r, leftChildIndex(node), al, mid);
    T rightValue = query(l, r, rightChildIndex(node), mid, ar);
    return op(leftValue, rightValue);
  }

 private:
  std::vector<T> data;
  int leafNum;
  T id;
  std::function<T(T, T)> op;
  std::function<T(T, T)> updateOp;
};
using namespace std;
template <class T>
void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\n";
}
template <class T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, T b) {
  if (a < b) a = b;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<SegmentTree<int>> row(
      n, SegmentTree<int>(
             n, 0, [](int a, int b) -> int { return a + b; },
             [](int _, int b) -> int { return b; }));
  vector<SegmentTree<int>> col(
      n, SegmentTree<int>(
             n, 0, [](int a, int b) -> int { return a + b; },
             [](int _, int b) -> int { return b; }));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      row[i].update(j, 1);
      col[i].update(j, 1);
    }
  }
  int ans = 0;
  for (int num = 0; num < n * n; num++) {
    int p;
    cin >> p;
    p--;
    int i = p / n;
    int j = p % n;
    row[i].update(j, 0);
    col[j].update(i, 0);
    int tmp = 501;
    chmin(tmp, row[i].find(0, j));
    chmin(tmp, row[i].find(j + 1, n));
    chmin(tmp, col[j].find(0, i));
    chmin(tmp, col[j].find(i + 1, n));
    ans += tmp;
  }
  cout << ans << endl;
}
