#include <bits/stdc++.h>
using namespace std;
const long long P = 998244353;
const int N = 505;
mt19937 rng(time(0));
int n, x, y, a[N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
void update(int x, int y) {
  v[x][y] = 0;
  queue<array<int, 2> > Q;
  Q.push({x, y});
  while (!Q.empty()) {
    array<int, 2> ele = Q.front();
    Q.pop();
    int x = ele[0], y = ele[1];
    for (int i = (0); i <= (3); ++i) {
      int x1 = x + dx[i], y1 = y + dy[i];
      if (dp[x][y] + v[x][y] < dp[x1][y1]) {
        dp[x1][y1] = dp[x][y] + v[x][y];
        Q.push({x1, y1});
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n * n); ++i) scanf("%d", a + i);
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j)
      dp[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), v[i][j] = 1;
  int ans = 0;
  for (int q = (1); q <= (n * n); ++q) {
    x = a[q] - 1;
    y = x % n + 1, x = x / n + 1;
    ans += dp[x][y];
    update(x, y);
  }
  cout << ans << endl;
  return 0;
}
