#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
using Pa = pair<T, U>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
int dp[4][510][510] = {};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vec<int> P(N * N);
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
  }
  vec<int> dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
  int inf = 1e9;
  vvec<int> mi(N, vec<int>(N, inf));
  vvec<int> vis(N, vec<int>(N, 1));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < 4; k++) {
        if (k == 0) dp[k][i][j] = i;
        if (k == 1) dp[k][i][j] = j;
        if (k == 2) dp[k][i][j] = N - i - 1;
        if (k == 3) dp[k][i][j] = N - j - 1;
        mi[i][j] = min(mi[i][j], dp[k][i][j]);
      }
  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };
  ll ans = 0;
  for (auto& p : P) {
    int x = p / N, y = p % N;
    int res = mi[x][y];
    for (int k = 0; k < 4; k++) {
      int nx = x, ny = y;
      int c = 0;
      while (true) {
        nx += dx[k], ny += dy[k];
        if (!in(nx, ny)) break;
        c += vis[nx][ny];
        res = min(res, c + mi[nx][ny]);
      }
    }
    ans += res;
    mi[x][y] = res;
    for (int k = 0; k < 4; k++) {
      int nx = x, ny = y;
      int tk = (k + 2) % 4;
      int c = 0;
      while (true) {
        nx += dx[k], ny += dy[k];
        if (!in(nx, ny)) break;
        dp[tk][nx][ny] = min(dp[tk][nx][ny], res + c);
        mi[nx][ny] = min(mi[nx][ny], dp[tk][nx][ny]);
        c += vis[nx][ny];
      }
    }
    vis[x][y] = 0;
  }
  cout << ans << "\n";
}
