#include <bits/stdc++.h>
namespace IO {
char gc() { return getchar(); }
template <typename Tp>
bool get1(Tp &x) {
  bool neg = 0;
  char c = gc();
  while (c != EOF && (c < '0' || c > '9') && c != '-') c = gc();
  if (c == '-') c = gc(), neg = 1;
  if (c == EOF) return false;
  x = 0;
  for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + c - '0';
  if (neg) x = -x;
  return true;
}
template <typename Tp>
void printendl(Tp x) {
  if (x < 0) putchar('-'), x = -x;
  static short a[40], sz;
  sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
  puts("");
}
}  // namespace IO
using IO::get1;
using IO::printendl;
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const int maxn = 511;
int n, p[maxn * maxn], dist[maxn][maxn], a[maxn][maxn];
void update(int x, int y) {
  std::deque<std::pair<int, int> > q;
  static int mark[maxn][maxn], timer;
  timer++;
  q.emplace_back(x, y);
  a[x][y] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    if (mark[x][y] == timer) continue;
    mark[x][y] = timer;
    if (dist[x - 1][y] > dist[x][y] + a[x][y]) {
      dist[x - 1][y] = dist[x][y] + a[x][y];
      if (a[x][y])
        q.emplace_back(x - 1, y);
      else
        q.emplace_front(x - 1, y);
    }
    if (dist[x + 1][y] > dist[x][y] + a[x][y]) {
      dist[x + 1][y] = dist[x][y] + a[x][y];
      if (a[x][y])
        q.emplace_back(x + 1, y);
      else
        q.emplace_front(x + 1, y);
    }
    if (dist[x][y - 1] > dist[x][y] + a[x][y]) {
      dist[x][y - 1] = dist[x][y] + a[x][y];
      if (a[x][y])
        q.emplace_back(x, y);
      else
        q.emplace_front(x, y - 1);
    }
    if (dist[x][y + 1] > dist[x][y] + a[x][y]) {
      dist[x][y + 1] = dist[x][y] + a[x][y];
      if (a[x][y])
        q.emplace_back(x, y);
      else
        q.emplace_front(x, y + 1);
    }
  }
}
int main() {
  get1(n);
  for (int i = 1; i <= n * n; i++) get1(p[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      dist[i][j] = std::min(i - 1, j - 1);
      dist[i][j] = std::min(dist[i][j], std::min(n - i, n - j));
      a[i][j] = 1;
    }
  long long ans = 0;
  for (int t = 1; t <= n * n; t++) {
    int i = (p[t] - 1) / n + 1, j = (p[t] - 1) % n + 1;
    ans += dist[i][j];
    update(i, j);
  }
  printendl(ans);
  return 0;
}
