#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 99824353;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 99824353;
    a = 1LL * a * a % 99824353;
    b >>= 1;
  }
  return ans;
}
int pow(int a, long long b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
int a[501][501], p[501 * 501], l[501][501], u[501][501];
int main() {
  int ca = 0, T, i, j, k, m, K, n;
  scanf("%d", &n);
  m = n * n;
  for (int i = 0; i < m; i++) {
    scanf("%d", &p[i]);
    p[i]--;
    a[p[i] / n][p[i] % n] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      l[i][j + 1] = l[i][j] + 1;
      u[i + 1][j] = u[i][j] + 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int first = p[i] / n, second = p[i] % n;
    int U = u[first][second], D = u[n][second] - u[first + 1][second];
    int L = l[first][second], R = l[first][n] - l[first][second + 1];
    ans += min(min(U, D), min(L, R));
    for (int j = second; j < n; j++) l[first][j + 1]--;
    for (int j = first; j < n; j++) u[j + 1][second]--;
  }
  printf("%d\n", ans);
}
