#include <bits/stdc++.h>
using namespace std;
template <typename Int>
inline Int read() {
  Int flag = 1;
  char c = getchar();
  while ((!isdigit(c)) && c != '-') c = getchar();
  if (c == '-') flag = -1, c = getchar();
  Int init = c & 15;
  while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}
template <typename Int>
inline Int read(char &c) {
  Int flag = 1;
  c = getchar();
  while ((!isdigit(c)) && c != '-') c = getchar();
  if (c == '-') flag = -1, c = getchar();
  Int init = c & 15;
  while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}
template <typename Int>
inline void write(Int x) {
  if (x < 0) putchar('-'), x = ~x + 1;
  if (x > 9) write(x / 10);
  putchar((x % 10) | 48);
}
template <typename Int>
inline void write(Int x, char nextch) {
  write(x);
  putchar(nextch);
}
int n;
int P;
pair<int, int> Get(int Pos) {
  return make_pair((Pos + n - 1) / n, (Pos - 1) % n + 1);
}
int rx[507], ry[507], tx[507][507], ty[507][507];
int ans = 0;
void upd(int *t, int pos) {
  while (pos <= n) {
    t[pos]++;
    pos += pos & (-pos);
  }
}
int sum(int *t, int pos) {
  int ret(0);
  while (pos) {
    ret += t[pos];
    pos -= pos & (-pos);
  }
  return ret;
}
int main() {
  n = read<int>();
  for (int i = 1; i <= n; i++) rx[i] = ry[i] = n;
  for (int i = 1; i <= n * n; i++) {
    P = read<int>();
    pair<int, int> p = Get(P);
    ans += min(min(p.first - sum(tx[p.second], p.first) - 1,
                   ry[p.second] - p.first + sum(tx[p.second], p.first)),
               min(p.second - sum(ty[p.first], p.second) - 1,
                   rx[p.first] - p.second + sum(ty[p.first], p.second)));
    rx[p.first]--;
    ry[p.second]--;
    upd(tx[p.second], p.first);
    upd(ty[p.first], p.second);
  }
  write(ans, 10);
  return 0;
}
