#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
long long N;
struct pos {
  long long x, y;
  bool operator<(const pos& a) const {
    if (x == a.x)
      return y < a.y;
    else
      return x < a.x;
  }
};
pos mpos(long long x, long long y) {
  pos tmp = {x, y};
  return tmp;
}
map<pos, long long> grid;
map<pos, bool> gridbool;
void del(pos now, long long c);
void update(pos now, long long c) {
  grid[now] = c;
  for (long long i = 0; i < 4; i++) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[nxt] > grid[now] + 1) {
        if (gridbool[nxt])
          update(nxt, grid[now] + 1);
        else
          del(nxt, grid[now] + 1);
      }
    }
  }
}
void del(pos now, long long c) {
  grid[now] = c;
  for (long long i = 0; i < 4; i++) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[nxt] > grid[now]) {
        if (gridbool[nxt])
          update(nxt, grid[now]);
        else
          del(nxt, grid[now]);
      }
    }
  }
}
int main() {
  cin >> N;
  vector<pos> p(N * N);
  for (long long i = 0; i < N * N; i++) {
    long long a;
    cin >> a;
    long long tmp;
    if (a % N == 0)
      tmp = N;
    else
      tmp = a % N;
    p[i].x = tmp - 1;
    p[i].y = (a - 1) / N;
  }
  long long ans = 0;
  for (long long i = 0; i < N; i++)
    for (long long j = 0; j < N; j++) {
      pos tmp = mpos(i, j);
      grid[tmp] = min(min(N - 1 - i, i), min(N - 1 - j, j));
      gridbool[tmp] = true;
    }
  for (long long i = 0; i < N * N; i++) {
    ans += grid[p[i]];
    gridbool[p[i]] = false;
    del(p[i], grid[p[i]]);
  }
  cout << ans;
  return 0;
}
