#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  char ch = getchar();
  x = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
const int N = 538;
int n, cx[N][N], cy[N][N], res;
inline void add_x(int k, int x, int v) {
  for (; x <= n; x += (x & -x)) cx[k][x] += v;
}
inline void add_y(int k, int x, int v) {
  for (; x <= n; x += (x & -x)) cy[k][x] += v;
}
inline int ask_x(int k, int x) {
  int res = 0;
  for (; x; x -= (x & -x)) res += cx[k][x];
  return res;
}
inline int ask_y(int k, int x) {
  int res = 0;
  for (; x; x -= (x & -x)) res += cy[k][x];
  return res;
}
signed main() {
  read(n);
  int tmp;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) add_x(i, j, 1), add_y(j, i, 1);
  for (int i = 1, x, y; i <= n * n; ++i) {
    read(tmp);
    x = tmp / n + 1, y = tmp % n;
    if (!y) y = n;
    add_x(x, y, -1), add_y(y, x, -1);
    res += min(ask_x(x, y - 1),
               min(ask_x(x, n) - ask_x(x, y),
                   min(ask_y(y, x - 1), ask_y(y, n) - ask_y(y, x))));
  }
  return printf("%d\n", res), 0;
}
