#include <bits/stdc++.h>
using namespace std;
vector<int> t1(4 * 2.5e5, 0), t2(4 * 2.5e5, 0);
void build(int n, int b, int e) {
  if (b == e) {
    t1[n] = 1;
    t2[n] = 1;
  } else {
    build(2 * n, b, (b + e) / 2);
    build(2 * n + 1, (b + e) / 2 + 1, e);
    t1[n] = t1[2 * n] + t1[2 * n + 1];
    t2[n] = t2[2 * n] + t2[2 * n + 1];
  }
}
void update1(int n, int b, int e, int k) {
  if (e < b) return;
  if (b == e) {
    t1[n] = 0;
    return;
  } else {
    if (k <= (b + e) / 2)
      update1(2 * n, b, (b + e) / 2, k);
    else
      update1(2 * n + 1, (b + e) / 2 + 1, e, k);
    t1[n] = t1[2 * n] + t1[2 * n + 1];
  }
}
void update2(int n, int b, int e, int k) {
  if (e < b) return;
  if (b == e) {
    t2[n] = 0;
  } else {
    if (k <= (b + e) / 2)
      update2(2 * n, b, (b + e) / 2, k);
    else
      update2(2 * n + 1, (b + e) / 2 + 1, e, k);
    t2[n] = t2[2 * n] + t2[2 * n + 1];
  }
}
int get1(int n, int b, int e, int i, int j) {
  if (e < i or b > j) return 0;
  if (i <= b and e <= j) {
    return t1[n];
  }
  return get1(2 * n, b, (b + e) / 2, i, j) +
         get1(2 * n + 1, (b + e) / 2 + 1, e, i, j);
}
int get2(int n, int b, int e, int i, int j) {
  if (e < i or b > j) return 0;
  if (i <= b and e <= j) {
    return t2[n];
  }
  return get2(2 * n, b, (b + e) / 2, i, j) +
         get2(2 * n + 1, (b + e) / 2 + 1, e, i, j);
}
int main() {
  int n;
  cin >> n;
  map<int, int> m;
  int k = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      m[k++] = (j - 1) * n + i;
    }
  }
  build(1, 1, n * n);
  int ans = 0;
  int c = n * n;
  while (c--) {
    int x;
    cin >> x;
    if (x <= n or x > (n - 1) * n or x % n == 1 or x % n == 0) {
      update1(1, 1, n * n, x);
      update2(1, 1, n * n, m[x]);
    } else {
      ans += min(get1(1, 1, n * n, n * (x / n) + 1, x - 1),
                 min(get1(1, 1, n * n, x + 1, (x / n + 1) * n),
                     min(get2(1, 1, n * n, n * (m[x] / n) + 1, m[x] - 1),
                         get2(1, 1, n * n, m[x] + 1, (m[x] / n + 1) * n))));
      update1(1, 1, n * n, x);
      update2(1, 1, n * n, m[x]);
      ;
    }
  }
  cout << ans << '\n';
}
