#include <bits/stdc++.h>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int n;
int p[2505];
int dp[505][505];
int ok[505][505];
int ans = 0;
int cur;
int getMin(int ro, int col) {
  int res = n;
  res = min(res, dp[ro - 1][col] + ok[ro - 1][col]);
  res = min(res, dp[ro + 1][col] + ok[ro + 1][col]);
  res = min(res, dp[ro][col - 1] + ok[ro][col - 1]);
  res = min(res, dp[ro][col + 1] + ok[ro][col + 1]);
  return res;
}
void process(int ro, int col);
void tryToUpdate(int ro, int col, int val) {
  if (dp[ro][col] <= val) return;
  dp[ro][col] = val;
  process(ro, col);
}
void process(int ro, int col) {
  int need = getMin(ro, col) + ok[ro][col];
  if (ro > 1) tryToUpdate(ro - 1, col, need);
  if (ro < n) tryToUpdate(ro + 1, col, need);
  if (col > 1) tryToUpdate(ro, col - 1, need);
  if (col < n) tryToUpdate(ro, col + 1, need);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 1; i <= n * n; i++) cin >> p[i];
  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;
    dp[ro][col] = min(min(ro - 1, n - ro), min(col - 1, n - col));
    ok[ro][col] = 1;
  }
  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;
    ans += dp[ro][col];
    ok[ro][col] = 0;
    process(ro, col);
  }
  cout << ans << endl;
  return 0;
}
