#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long power(long long first, unsigned long long second,
                unsigned long long m) {
  if (second == 0) return 1;
  long long p = power(first, second / 2, m) % m;
  p = (p * p) % m;
  return (second % 2 == 0) ? p : (first * p) % m;
}
long long modInverse(long long a, long long m) {
  long long g = gcd(a, m);
  { return power(a, m - 2, m); }
}
long long dp[505][505];
bool vis[505][505];
long long n;
long long first[] = {1, -1, 0, 0};
long long second[] = {0, 0, 1, -1};
void dfs(long long xx, long long yy) {
  long long z = dp[xx][yy] + vis[xx][yy];
  for (long long k = 0; k < 4; k++) {
    if ((first[k] + xx <= n) && (first[k] + xx > 0) && (second[k] + yy <= n) &&
        (second[k] + yy > 0)) {
      if ((dp[first[k] + xx][second[k] + yy] > z)) {
        dp[first[k] + xx][second[k] + yy] = z;
        dfs(first[k] + xx, second[k] + yy);
      }
    }
  }
}
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      dp[i][j] = min(min(j - 1, n - j), min(i - 1, n - i));
    }
  }
  memset(vis, true, sizeof vis);
  long long ans = 0;
  for (long long i = 0; i < n * n; i++) {
    long long where;
    cin >> where;
    long long whx = (where + n - 1) / n;
    long long why = where % n;
    ans += dp[whx][why];
    vis[whx][why] = false;
    dfs(whx, why);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
