#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
long long MOD = 998244353;
long long mpow(long long a, long long b) {
  if (b >= (MOD - 1)) b %= (MOD - 1);
  if (b == 0) return 1;
  long long t1 = mpow(a, b / 2);
  t1 *= t1;
  t1 %= MOD;
  if (b % 2) t1 *= a;
  t1 %= MOD;
  return t1;
}
long long mpow(long long a, long long b, long long p) {
  if (b == 0) return 1;
  long long t1 = mpow(a, b / 2, p);
  t1 *= t1;
  t1 %= p;
  if (b % 2) t1 *= a;
  t1 %= p;
  return t1;
}
long long modinverse(long long a, long long m) {
  long long m0 = m;
  long long y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long q = a / m;
    long long t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long range(long long l, long long r) { return l + rand() % (r - l + 1); }
vector<vector<int>> per(505, vector<int>(505, 0));
vector<vector<int>> dif(505, vector<int>(505, 30000));
int n;
void updrow(int r) {
  for (int i = 1; i <= n; i++) {
    dif[r][i] = min(dif[r][i - 1] + per[r][i - 1], dif[r][i]);
  }
  for (int i = n; i > 0; i--) {
    dif[r][i] = min(dif[r][i], dif[r][i + 1] + per[r][i + 1]);
  }
}
void updcol(int c) {
  for (int i = 1; i <= n; i++) {
    dif[i][c] = min(dif[i - 1][c] + per[i - 1][c], dif[i][c]);
  }
  for (int i = n; i > 0; i--) {
    dif[i][c] = min(dif[i][c], dif[i + 1][c] + per[i + 1][c]);
  }
}
void init() {
  for (int i = 0; i <= n + 1; i++) dif[0][i] = 0;
  for (int i = 0; i <= n + 1; i++) dif[n][i] = 0;
  for (int i = 0; i <= n + 1; i++) dif[i][0] = 0;
  for (int i = 0; i <= n + 1; i++) dif[i][n] = 0;
  for (int i = 0; i <= n + 1; i++) dif[n + 1][i] = 0;
  for (int i = 0; i <= n + 1; i++) dif[i][n + 1] = 0;
}
int main() {
  cin >> n;
  int arr[n * n];
  for (int i = 0; i < n * n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) per[i][j] = 1;
  }
  init();
  for (int i = 1; i <= n; i++) {
    updcol(i);
  }
  for (int i = 1; i <= n; i++) updrow(i);
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int r = (arr[i] / n) + 1;
    int c = (arr[i] % n);
    if (arr[i] % n == 0) r--, c = n;
    ans += dif[r][c];
    per[r][c] = 0;
    updcol(c);
    updrow(r);
    for (int i = 1; i <= n; i++) updrow(i);
    for (int i = 1; i <= n; i++) updcol(i);
  }
  cout << ans << endl;
}
