#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> dyx[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int cost[500][500], exist[500][500];
void update(int sy, int sx) {
  queue<pair<int, int>> q;
  q.push(make_pair(sy, sx));
  while (!q.empty()) {
    pair<int, int> front = q.front();
    q.pop();
    int y = front.first;
    int x = front.second;
    int cost2[4];
    for (int i = 0; i < 4; i++) {
      int y2 = y + dyx[i].first;
      int x2 = x + dyx[i].second;
      if (y2 < 0 || y2 >= n || x2 < 0 || x2 >= n) {
        cost2[i] = 1e9;
        continue;
      }
      cost2[i] = cost[y2][x2] + exist[y2][x2];
    }
    for (int i = 0; i < 4; i++) {
      int y2 = y + dyx[i].first;
      int x2 = x + dyx[i].second;
      if (y2 < 0 || y2 >= n || x2 < 0 || x2 >= n) {
        continue;
      }
      bool updated = false;
      for (int j = 0; j < 4; j++) {
        if (i == j) {
          continue;
        }
        if (cost2[j] < cost[y2][x2]) {
          cost[y2][x2] = cost2[j];
          updated = true;
        }
      }
      if (updated) {
        q.push(make_pair(y2, x2));
      }
    }
  }
}
int main() {
  cin >> n;
  int p[n * n];
  for (int i = 0; i < n * n; i++) cin >> p[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cost[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
      exist[i][j] = 1;
    }
  }
  long long ans = 0;
  for (int pi : p) {
    --pi;
    int y = pi / n;
    int x = pi % n;
    ans += cost[y][x];
    --exist[y][x];
    update(y, x);
  }
  int cnt = 0;
  cout << ans << endl;
}
