#include <bits/stdc++.h>
using namespace std;
int n, p, x, y, ans, var[4] = {-1, 1, 0, 0}, hol[4] = {0, 0, -1, 1};
bool path[505][505], emp[505][505], visit[505][505];
void make() {
  for (int i = 1; i <= n * n; i++) {
    x = (i - 1) / n, y = (i - 1) % n;
    path[x][y] = emp[x][y] = false;
  }
  for (int i = 0; i <= n + 1; i++) {
    path[0][i] = path[n + 1][i] = path[i][n + 1] = path[i][n + 1] = emp[0][i] =
        emp[n + 1][i] = emp[i][0] = emp[i][n + 1] = true;
  }
}
void dive1(int x, int y) {
  path[x][y] = true;
  for (int i = 0; i < 4; i++) {
    if (path[x + var[i]][y + hol[i]] == false &&
        emp[x + var[i]][y + hol[i]] == true) {
      dive1(x + var[i], y + hol[i]);
    }
  }
}
int dive2(int x, int y) {
  int z = 1;
  while (true) {
    for (int i = 0; i <= z; i++) {
      printf("%d %d  %d %d\n", x + i, y + z - i, x - i, y - z + i);
      if (path[x + i][y + z - i] || path[x - i][y - z + i]) {
        return (z);
      }
    }
    z++;
  }
}
void chack1() {
  cout << p << endl;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= n + 1; j++) {
      cout << path[i][j];
    }
    cout << ' ';
    for (int j = 0; j <= n + 1; j++) {
      if (i == x && j == y) {
        cout << '*';
      } else {
        cout << emp[i][j];
      }
    }
    cout << endl;
  }
  cout << ans << endl << endl;
}
signed main() {
  cin >> n;
  make();
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    x = (p - 1) / n + 1, y = (p - 1) % n + 1;
    emp[x][y] = true;
    if (path[x - 1][y] || path[x + 1][y] || path[x][y - 1] || path[x][y + 1]) {
      dive1(x, y);
    } else {
      ans += dive2(x, y);
    }
  }
  cout << ans << endl;
  return (0);
}
