#include <bits/stdc++.h>
#pragma GCC optimize 2
using namespace std;
short dis[505][505], a[505][505];
int p[250005];
queue<int> q1, q2;
int main(int argc, char** argv) {
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n * n; i++) scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  for (int i = 1; i <= n * n; i++) {
    int x = p[i];
    q1.push(x), q2.push(x);
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) dis[j][k] = 1000;
    dis[(x - 1) / n + 1][x - ((x - 1) / n) * n] = 0;
    a[(x - 1) / n + 1][x - ((x - 1) / n) * n] = 0;
    int flag = 0;
    while (!flag) {
      while (!q1.empty()) {
        int t = q1.front(), x = (t - 1) / n + 1, y = t - (x - 1) * n;
        q1.pop();
        q2.push(t);
        if (x == 1 || x == n || y == 1 || y == n) {
          flag = 1;
          ans += dis[x][y];
          while (!q1.empty()) q1.pop();
          while (!q2.empty()) q2.pop();
          break;
        }
        if (!a[x - 1][y] && dis[x - 1][y] == 1000)
          q1.push((x - 2) * n + y), dis[x - 1][y] = dis[x][y];
        if (!a[x + 1][y] && dis[x + 1][y] == 1000)
          q1.push((x)*n + y), dis[x + 1][y] = dis[x][y];
        if (!a[x][y - 1] && dis[x][y - 1] == 1000)
          q1.push((x - 1) * n + y - 1), dis[x][y - 1] = dis[x][y];
        if (!a[x][y + 1] && dis[x][y + 1] == 1000)
          q1.push((x - 1) * n + y + 1), dis[x][y + 1] = dis[x][y];
      }
      while (!q2.empty()) {
        int t = q2.front(), x = (t - 1) / n + 1, y = t - (x - 1) * n;
        q2.pop();
        if (x == 1 || x == n || y == 1 || y == n) {
          flag = 1;
          ans += dis[x][y];
          while (!q1.empty()) q1.pop();
          while (!q2.empty()) q2.pop();
          break;
        }
        if (a[x - 1][y] && dis[x - 1][y] == 1000)
          q1.push((x - 2) * n + y), dis[x - 1][y] = dis[x][y] + 1;
        if (a[x + 1][y] && dis[x + 1][y] == 1000)
          q1.push((x)*n + y), dis[x + 1][y] = dis[x][y] + 1;
        if (a[x][y - 1] && dis[x][y - 1] == 1000)
          q1.push((x - 1) * n + y - 1), dis[x][y - 1] = dis[x][y] + 1;
        if (a[x][y + 1] && dis[x][y + 1] == 1000)
          q1.push((x - 1) * n + y + 1), dis[x][y + 1] = dis[x][y] + 1;
      }
    }
  }
  cout << ans;
  return 0;
}
