#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline char CH() {
  static char buf[100000], *ptr1 = buf, *ptr2 = buf;
  return ptr1 == ptr2 && (ptr2 = (ptr1 = buf) + fread(buf, 1, 100000, stdin),
                          ptr1 == ptr2)
             ? EOF
             : *ptr1++;
}
static inline int IN(void) {
  int x = 0, f = 0, c = CH();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = CH();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = CH();
  }
  return f ? -x : x;
}
static inline void OUT(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) OUT(x / 10);
  putchar(x % 10 + 48);
}
static inline int min(const int a, const int b) { return a < b ? a : b; }
int main(void) {
  int N = IN(), a = 0, across, d, down, p, q, xy, que[251502], vacant[252500],
      dist[252500];
  for (int i = 0; i < (int)N; i++) {
    xy = (N + 2) * (i + 1) + 1;
    across = min(i, N - 1 - i), down = 3;
    for (int j = 0; j < (int)across; j++) {
      dist[xy++] = down++;
    }
    for (int j = 0; j < (int)N - 2 * across; j++) {
      dist[xy++] = across + 3;
    }
    for (int j = 0; j < (int)across; j++) {
      dist[xy++] = --down;
    }
  }
  for (int k = 0; k < (int)N * N; k++) {
    xy = IN();
    xy += (xy - 1) / N * 2 + N + 2;
    a += dist[xy];
    p = 0, q = 0;
    que[p++] = xy;
    vacant[xy] = 1;
    while (p > q) {
      xy = que[q++], d = dist[xy] - vacant[xy] + 1;
      if (dist[xy - N - 2] > d) {
        dist[xy - N - 2] = d;
        que[p++] = xy - N - 2;
      }
      if (dist[xy + N + 2] > d) {
        dist[xy + N + 2] = d;
        que[p++] = xy + N + 2;
      }
      if (dist[xy - 1] > d) {
        dist[xy - 1] = d;
        que[p++] = xy - 1;
      }
      if (dist[xy + 1] > d) {
        dist[xy + 1] = d;
        que[p++] = xy + 1;
      }
    }
  }
  OUT(a - 3 * N * N);
  return puts(""), 0;
}
