#include <bits/stdc++.h>
namespace IO {
using namespace std;
char buf[1 << 22], Out[1 << 22], *p1 = buf, *p2 = buf;
int p3 = -1, f = 0;
inline void file() {}
inline char getchar() {
  return (p1 == p2) &&
                 (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void flush() { fwrite(Out, 1, p3 + 1, stdout), p3 = -1; }
inline void check() {
  if (p3 > (1 << 21)) flush();
}
inline void putchar(char a) {
  Out[++p3] = a;
  check();
}
inline void putline(string s) {
  int tmp = p3;
  while (p3 - tmp < s.size()) putchar(s[p3 - tmp]);
  putchar('\n');
}
inline char gc() {
  char c = getchar();
  while (!c || c == '\n' || c == ' ') c = getchar();
  return c;
}
}  // namespace IO
namespace my_std {
using namespace std;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
const int INF = 0x7fffffff;
inline int read() {
  int sum = 0, f = 0;
  char c = IO::getchar();
  while (!isdigit(c)) f |= (c == '-'), c = IO::getchar();
  while (isdigit(c))
    sum = (sum << 1) + (sum << 3) + (c ^ 48), c = IO::getchar();
  return f ? -sum : sum;
}
void write(int k) {
  if (k < 0) IO::putchar('-'), k = -k;
  if (k >= 10) write(k / 10);
  IO::putchar(k % 10 + 48);
}
template <typename T>
inline void chkmax(T &x, T y) {
  if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
  if (x > y) x = y;
}
}  // namespace my_std
using namespace my_std;
const int N = 1010;
int n, ans, a[N * N], dis[N][N], dlt[N][N];
int fx[5][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
void dfs(int x, int y) {
  for (register int i = (0); i <= (3); i++) {
    int xx = x + fx[i][0], yy = y + fx[i][1];
    if (xx >= 1 && xx <= n && yy >= 1 && yy <= n &&
        dis[x][y] + 1 - dlt[xx][yy] < dis[xx][yy]) {
      chkmin(dis[xx][yy], dis[x][y] + 1 - dlt[xx][yy]);
      dfs(xx, yy);
    }
  }
}
int main() {
  IO::file();
  n = read();
  for (register int i = (1); i <= (n * n); i++) a[i] = read();
  for (register int i = (1); i <= (n); i++)
    for (register int j = (1); j <= (n); j++)
      dis[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
  for (register int i = (1); i <= (n * n); i++) {
    int x = (a[i] - 1) / n + 1, y = a[i] - n * x + n;
    ans += dis[x][y] - 1;
    for (register int j = (0); j <= (3); j++) {
      int xx = x + fx[j][0], yy = y + fx[j][1];
      if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
        chkmin(dis[x][y], dis[xx][yy]);
      }
    }
    dlt[x][y] = 1;
    dfs(x, y);
  }
  write(ans), IO::putchar('\n'), IO::check();
  IO::flush();
  return 0;
}
