#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long int lazyr[3005][3005];
long long int treer[3005][3005];
long long int lazyc[3005][3005];
long long int treec[3005][3005];
void updater(int row, int node, int start, int end, int l, int r, int val) {
  if (lazyr[row][node] != 0) {
    treer[row][node] += (end - start + 1) * lazyr[row][node];
    if (start != end) {
      lazyr[row][node * 2] += lazyr[row][node];
      lazyr[row][2 * node + 1] += lazyr[row][node];
    }
    lazyr[row][node] = 0;
  }
  if (start > end or start > r or end < l) return;
  if (start >= l and end <= r) {
    treer[row][node] += (end - start + 1) * val;
    if (start != end) {
      lazyr[row][node * 2] += val;
      lazyr[row][2 * node + 1] += val;
    }
    return;
  }
  int mid = (start + end) / 2;
  updater(row, node * 2, start, mid, l, r, val);
  updater(row, node * 2 + 1, mid + 1, end, l, r, val);
  treer[row][node] = treer[row][node * 2] + treer[row][node * 2 + 1];
}
void updatec(int col, int node, int start, int end, int l, int r, int val) {
  if (lazyc[col][node] != 0) {
    treec[col][node] += (end - start + 1) * lazyc[col][node];
    if (start != end) {
      lazyc[col][node * 2] += lazyc[col][node];
      lazyc[col][2 * node + 1] += lazyc[col][node];
    }
    lazyc[col][node] = 0;
  }
  if (start > end or start > r or end < l) return;
  if (start >= l and end <= r) {
    treec[col][node] += (end - start + 1) * val;
    if (start != end) {
      lazyc[col][node * 2] += val;
      lazyc[col][2 * node + 1] += val;
    }
    return;
  }
  int mid = (start + end) / 2;
  updatec(col, node * 2, start, mid, l, r, val);
  updatec(col, node * 2 + 1, mid + 1, end, l, r, val);
  treec[col][node] = treec[col][node * 2] + treec[col][node * 2 + 1];
}
int queryr(int row, int node, int start, int end, int l, int r) {
  if (start > end or start > r or end < l) return 0;
  if (lazyr[row][node] != 0) {
    treer[row][node] += (end - start + 1) * lazyr[row][node];
    if (start != end) {
      lazyr[row][node * 2] += lazyr[row][node];
      lazyr[row][node * 2 + 1] += lazyr[row][node];
    }
    lazyr[row][node] = 0;
  }
  if (start >= l and end <= r) return treer[row][node];
  int mid = (start + end) / 2;
  int p1 = queryr(row, node * 2, start, mid, l, r);
  int p2 = queryr(row, node * 2 + 1, mid + 1, end, l, r);
  return (p1 + p2);
}
int queryc(int col, int node, int start, int end, int l, int r) {
  if (start > end or start > r or end < l) return 0;
  if (lazyc[col][node] != 0) {
    treec[col][node] += (end - start + 1) * lazyc[col][node];
    if (start != end) {
      lazyc[col][node * 2] += lazyc[col][node];
      lazyc[col][node * 2 + 1] += lazyc[col][node];
    }
    lazyc[col][node] = 0;
  }
  if (start >= l and end <= r) return treec[col][node];
  int mid = (start + end) / 2;
  int p1 = queryc(col, node * 2, start, mid, l, r);
  int p2 = queryc(col, node * 2 + 1, mid + 1, end, l, r);
  return (p1 + p2);
}
long long int n;
long long int getr(long long int x) {
  long long int re = x / n;
  if (x % n) re++;
  return re;
}
long long int getc(long long int x) {
  long long int re = x % n;
  if (re == 0) re = n;
  return re;
}
long long int a[3005];
void solve() {
  cin >> n;
  for (long long int i = 1; i <= n * n; i++) {
    cin >> a[i];
  }
  for (long long int i = 1; i <= n; i++) {
    updater(i, 1, 1, n, 1, n, 1);
    updatec(i, 1, 1, n, 1, n, 1);
  }
  long long int an = 0;
  for (long long int i = 1; i <= n * n; i++) {
    long long int row = getr(a[i]);
    long long int col = getc(a[i]);
    long long int minnr =
        min(queryr(row, 1, 1, n, 1, col - 1), queryr(row, 1, 1, n, col + 1, n));
    long long int minnc =
        min(queryc(col, 1, 1, n, 1, row - 1), queryc(col, 1, 1, n, row + 1, n));
    long long int cL = queryc(col, 1, 1, n, 1, row - 1);
    long long int cR = queryc(col, 1, 1, n, row + 1, n);
    an += min(minnc, minnr);
    updater(row, 1, 1, n, col, col, -1);
    updatec(col, 1, 1, n, row, row, -1);
  }
  cout << an << " " << endl;
  ;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  fflush(stdout);
  long long int t = 1;
  for (long long int tcase = 1; tcase <= t; tcase++) {
    solve();
  }
  return 0;
}
