#include <bits/stdc++.h>
using namespace std;
int dist[505][505], n, p[250005];
bool f[505][505];
int cnt;
inline void Min(int& x, int y) {
  if (x > y) x = y;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = 1, dist[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    cnt += dist[x][y];
    f[x][y] = 0;
    for (int k = x - 1; k; --k) Min(dist[k][y], dist[k + 1][y] + f[k + 1][y]);
    for (int k = x + 1; k <= n; ++k)
      Min(dist[k][y], dist[k - 1][y] + f[k - 1][y]);
    for (int k = y - 1; k; --k) Min(dist[x][k], dist[x][k + 1] + f[x][k + 1]);
    for (int k = y + 1; k <= n; ++k)
      Min(dist[x][k], dist[x][k - 1] + f[x][k - 1]);
  }
  cout << cnt << '\n';
}
