#include <bits/stdc++.h>
using namespace std;
const int n = 505;
const int INF = 1e5;
int N;
bool occ[n][n];
int dp[n][n];
int x[4] = {0, 1, 0, -1};
int y[4] = {1, 0, -1, 0};
void printOcc() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) cout << occ[i][j];
    cout << endl;
  }
}
void printDp() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) cout << dp[i][j] << " ";
    cout << endl;
  }
}
void calc() {
  deque<int> Q;
  for (int i = 2; i < N; i++) {
    for (int j = 2; j < N; j++) dp[i][j] = INF;
  }
  for (int i = 1; i <= N; i++) {
    Q.push_back(i + N + 1);
    Q.push_back(i + N * (N + 1));
    Q.push_back(1 + i * (N + 1));
    Q.push_back(N + i * (N + 1));
  }
  while (!Q.empty()) {
    int r = Q.front() / (N + 1);
    int c = Q.front() % (N + 1);
    Q.pop_front();
    if (r > N || r < 1 || c < 1 || c > N) continue;
    for (int i = 0; i < 4; i++) {
      if (occ[r][c]) {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_back((r + y[i]) * (N + 1) + c + x[i]);
        }
      } else {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_front((r + y[i]) * (N + 1) + c + x[i]);
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) occ[i][j] = true;
  long long res = 0;
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    calc();
    cin >> P[i];
    P[i]--;
    res += dp[P[i] % N + 1][P[i] / N + 1];
    occ[P[i] % N + 1][P[i] / N + 1] = false;
  }
  cout << res << endl;
  return 0;
}
