#include <bits/stdc++.h>

using namespace std;

const int N = 505;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int n;
int P[N * N];
int dist[N][N];
bool occ[N][N];

inline bool isin(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < n;
}

void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i], ty = y + dy[i];
    if (isin(tx, ty) && dist[x][y] + occ[x][y] < dist[tx][ty]) {
      dist[tx][ty] = dist[x][y] + occ[x][y];
      dfs(tx, ty);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> P[i];
    --P[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      occ[i][j] = 1;
      dist[i][j] = min({i, j, n - i - 1, n - j - 1});
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int x = P[i] / n, y = P[i] % n;
    ans += dist[x][y];
    occ[x][y] = 0;
    dfs(x, y);
  }
  cout << ans << '\n';
  return 0;
}
