#include <bits/stdc++.h>
using namespace std;
int n;
int p[500 * 500];
int dist[500][500];
bool isVacant[500][500];
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dx2[] = {-1, 1, -1, 1};
const int dy2[] = {-1, -1, 1, 1};
void update(int x, int y) {
  if (!(x >= 0 && y >= 0 && x < n && y < n)) return;
  for (int i = 0, i_len = (4); i < i_len; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (isVacant[y][x] && dist[ny][nx] > dist[y][x]) {
      dist[ny][nx] = dist[y][x];
      update(nx, ny);
    }
    if (!isVacant[y][x] && dist[ny][nx] > dist[y][x]) {
      dist[ny][nx] = dist[y][x] + 1;
      update(nx, ny);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0, i_len = (n * n); i < i_len; ++i) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0, i_len = (n * n); i < i_len; ++i) {
    int x = i % n;
    int y = i / n;
    dist[y][x] = min({x, y, n - 1 - x, n - 1 - y});
  }
  long long ans = 0;
  for (int i = 0, i_len = (n * n); i < i_len; ++i) {
    int x = p[i] % n;
    int y = p[i] / n;
    ans += dist[y][x];
    isVacant[y][x] = true;
    for (int i = 0, i_len = (4); i < i_len; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (dist[ny][nx] >= dist[y][x] + 1) {
        dist[ny][nx] = dist[y][x];
        update(nx, ny);
      }
    }
    for (int i = 0, i_len = (4); i < i_len; ++i) {
      int nx = x + dx2[i], ny = y + dy2[i];
      if (dist[ny][nx] >= dist[y][x] + 2) {
        dist[ny][nx] = dist[y][x] + 1;
        update(nx, ny);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
