#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct BIT2D {
  int H, W;
  vector<vector<T>> bit;
  BIT2D(int H_, int W_) { init(H_, W_); }
  void init(int H_, int W_) {
    H = H_ + 1;
    W = W_ + 1;
    bit.assign(H, vector<T>(W, 0));
  }
  void add(int h, int w, T x) {
    for (int i = h; i < H; i += (i & -i)) {
      for (int j = w; j < W; j += (j & -j)) {
        bit[i][j] += x;
      }
    }
  }
  T sum(int h, int w) {
    T s(0);
    for (int i = h; i > 0; i -= (i & -i)) {
      for (int j = w; j > 0; j -= (j & -j)) {
        s += bit[i][j];
      }
    }
    return s;
  }
  T query(int h1, int w1, int h2, int w2) {
    return sum(h2 - 1, w2 - 1) - sum(h2 - 1, w1 - 1) - sum(h1 - 1, w2 - 1) +
           sum(h1 - 1, w1 - 1);
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  BIT2D<long long> b2(n, n);
  vector<long long> p(n * n);
  for (int i = 0; i < (int)(n * n); i++) {
    cin >> p[i];
  }
  long long ans = 0;
  for (int i = 1; i <= (int)(n); i++)
    for (int j = 1; j <= (int)(n); j++) {
      b2.add(i, j, 1LL);
    }
  for (int i = 0; i < (int)(n * n); i++) {
    int row = (p[i] - 1) % n + 1;
    int col = (p[i] - 1) / n + 1;
    long long res = b2.query(row, 1, row + 1, col);
    res = min(res, b2.query(row, col + 1, row + 1, n + 1));
    res = min(res, b2.query(1, col, row, col + 1));
    res = min(res, b2.query(row + 1, col, n + 1, col + 1));
    ans += res;
    b2.add(row, col, -1);
  }
  cout << ans << endl;
  return 0;
}
