#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
template <typename _T>
inline void read(_T &f) {
  f = 0;
  _T fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      fu = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= fu;
}
template <typename T>
void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10)
    putchar(x + 48);
  else
    print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
  print(x);
  putchar(t);
}
const int N = 505;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
struct ele {
  int x, y;
  ele(int a = 0, int b = 0) : x(a), y(b) {}
};
queue<ele> q;
int a[N][N], dis[N][N];
int n, ans;
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
      a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int v, x, y;
    read(v);
    x = (v - 1) / n + 1;
    y = (v - 1) % n + 1;
    --dis[x][y];
    a[x][y] = 0;
    ans += dis[x][y];
    q.push(ele(x, y));
    while (!q.empty()) {
      ele u = q.front();
      q.pop();
      for (int j = 0; j < 4; j++) {
        int _x = u.x + dx[j], _y = u.y + dy[j];
        if (dis[_x][_y] > dis[u.x][u.y] + a[_x][_y]) {
          dis[_x][_y] = dis[u.x][u.y] + a[_x][_y];
          q.push(ele(_x, _y));
        }
      }
    }
  }
  print(ans, '\n');
  return 0;
}
