#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using ii = pair<int, int>;
const int MAXN = 501;
const ii dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int dp[MAXN][MAXN];
bool isempty[MAXN][MAXN];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) {
    scanf("%d", &p[i]);
  }
  for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= n; col++) {
      dp[row][col] = min({row - 1, n - row, col - 1, n - col});
    }
  }
  lli ans = 0;
  for (int i = 0; i < n * n; i++) {
    int row = (p[i] - 1) / n + 1;
    int col = ((p[i] - 1) % n) + 1;
    ans += lli(dp[row][col]);
    isempty[row][col] = true;
    queue<ii> q;
    q.push({row, col});
    while (!q.empty()) {
      ii u = q.front();
      q.pop();
      for (int j = 0; j < 4; j++) {
        int nrow = u.first + dir[j].first;
        int ncol = u.second + dir[j].second;
        if (nrow < 1 or nrow > n or ncol < 1 or ncol > n) {
          continue;
        }
        if (dp[nrow][ncol] > dp[u.first][u.second]) {
          dp[nrow][ncol] = dp[u.first][u.second];
          if (isempty[nrow][ncol]) {
            q.push({nrow, ncol});
          }
        }
      }
    }
  }
  printf("%I64d\n", ans);
}
