#include <bits/stdc++.h>
using namespace std;
pair<int, int> seat(int s, int n) {
  int y = s / n;
  int x = s % n;
  return make_pair(y, x);
}
int main(void) {
  int n;
  cin >> n;
  vector<int> P;
  int p;
  for (int i = int(1); i <= int(n * n); ++i) {
    cin >> p;
    p--;
    P.push_back(p);
  }
  vector<vector<int>> L(n, vector<int>(n, 0));
  vector<vector<int>> R(n, vector<int>(n, 0));
  vector<vector<int>> U(n, vector<int>(n, 0));
  vector<vector<int>> D(n, vector<int>(n, 0));
  for (int i = int(0); i <= int(n - 1); ++i) {
    for (int j = int(0); j <= int(n - 1); ++j) {
      L[i][j] = j;
    }
  }
  for (int i = int(0); i <= int(n - 1); ++i) {
    for (int j = int(0); j <= int(n - 1); ++j) {
      R[i][j] = n - j - 1;
    }
  }
  for (int i = int(0); i <= int(n - 1); ++i) {
    for (int j = int(0); j <= int(n - 1); ++j) {
      U[i][j] = i;
    }
  }
  for (int i = int(0); i <= int(n - 1); ++i) {
    for (int j = int(0); j <= int(n - 1); ++j) {
      D[i][j] = n - i - 1;
    }
  }
  long long ans = 0;
  for (int i = int(0); i <= int(n * n - 1); ++i) {
    int y = seat(P[i], n).first;
    int x = seat(P[i], n).second;
    ans += min(L[y][x], min(R[y][x], min(U[y][x], D[y][x])));
    for (int j = int(x); j <= int(n - 1); ++j) {
      L[y][j] = max(L[y][j] - 1, 0);
    }
    for (int j = int(x); j >= int(0); j--) {
      R[y][j] = max(R[y][j] - 1, 0);
    }
    for (int j = int(y); j <= int(n - 1); ++j) {
      U[j][x] = max(U[j][x] - 1, 0);
    }
    for (int j = int(y); j >= int(0); j--) {
      D[j][x] = max(D[j][x] - 1, 0);
    }
  }
  cout << ans << endl;
  return 0;
}
