#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool mod(long double a, long double b) { return a / b - floor(a / b); }
const long long N = 55;
long long grid[N][N], vis[N][N], dis[N][N], n;
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
void dfs(long long d, long long first, long long second) {
  long long i, a, b, c, xx;
  for (i = 0; i < 4; i++) {
    a = first + dx[i];
    b = second + dy[i];
    if ((a < 1) || (a > n) || (b < 1) || (b > n)) continue;
    c = dis[a][b];
    xx = d + grid[first][second];
    if (c > xx) {
      dis[a][b] = xx;
      dfs(xx, a, b);
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, first;
  cin >> n;
  for (i = 1; i < n + 1; i++) {
    for (j = 1; j < n + 1; j++) {
      grid[i][j] = 1;
      vis[i][j] = -1;
      dis[i][j] = min({i - 1, n - i, j - 1, n - j});
    }
  }
  vector<long long> v;
  for (i = 0; i < n * n; i++) {
    cin >> first;
    v.push_back(first);
  }
  long long ans = 0;
  for (i = 0; i < n * n; i++) {
    long long a, b, c, d;
    first = v[i];
    a = ((first - 1) / n) + 1;
    b = ((first - 1) % n) + 1;
    ans += dis[a][b];
    grid[a][b] = 0;
    first = dis[a][b];
    for (j = 0; j < 4; j++) {
      c = a + dx[j];
      d = b + dy[j];
      if ((c < 1) || (c > n) || (d < 1) || (d > n)) continue;
      first = min(dis[c][d] + grid[c][d], first);
    }
    dis[a][b] = first;
    dfs(dis[a][b], a, b);
  }
  cout << ans << '\n';
  return 0;
}
