#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
int n;
vector<vector<bool>> is(550, vector<bool>(550, 1));
void cha(int y, int x, vector<vector<ll>> &v, int k, ll &c) {
  for (ll j = 0; j < (ll)4; j++) {
    int ny = y + dy[j];
    int nx = x + dx[j];
    if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
    if (v[ny][nx] > c + k) {
      v[ny][nx] = c + k;
      cha(ny, nx, v, k + is[ny][nx], c);
    }
  }
}
int main() {
  cin >> n;
  vector<vector<ll>> v(n, vector<ll>(n, 0));
  for (ll i = 0; i < (ll)n; i++)
    for (ll j = 0; j < (ll)n; j++) v[i][j] += min({i, j, n - i - 1, n - j - 1});
  int suu = n * n;
  ll res = 0;
  for (ll i = 0; i < (ll)suu; i++) {
    int p;
    scanf("%d", &p);
    int y = p / n, x = p % n - 1;
    if ((p % n) == 0) y--, x += n;
    res += v[y][x];
    cha(y, x, v, 0, v[y][x]);
  }
  cout << res << endl;
  return 0;
}
