#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n;
int p[N * N];
int viewer[N][N];
int dist[N][N];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      viewer[i][j] = 1;
      dist[i][j] = min(min(i + 1, j + 1), min(n - i, n - j));
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int p;
    scanf("%d", &p);
    --p;
    int x = p / n, y = p % n;
    viewer[x][y] = 0;
    ans += --dist[x][y];
    deque<pair<int, int>> q;
    q.push_back(make_pair(x, y));
    while (q.size()) {
      int x, y;
      tie(x, y) = q.front();
      q.pop_back();
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
        if (dist[nx][ny] <= dist[x][y] + viewer[nx][ny]) continue;
        dist[nx][ny] = dist[x][y] + viewer[nx][ny];
        if (viewer[nx][ny])
          q.push_back(make_pair(nx, ny));
        else
          q.push_front(make_pair(nx, ny));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
