#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> seat(N, vector<ll>(N, 0));
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      seat[i][j] = min({i, j, N - i - 1, N - j - 1});
    }
  }
  ll ans = 0;
  for (ll i = 0; i < N * N; i++) {
    ll pi;
    cin >> pi;
    const ll row = (pi - 1) / N;
    const ll col = (pi - 1) % N;
    ll cur = seat[row][col];
    {
      ll right = col;
      while (right < N - 1) {
        right += 1;
        if (seat[row][right] == -1) {
          continue;
        }
        if (cur >= seat[row][right] + 1) {
          cur = min(cur, seat[row][right] + 1);
          break;
        }
      }
    }
    {
      ll left = col;
      while (left > 0) {
        left -= 1;
        if (seat[row][left] == -1) {
          continue;
        }
        if (cur >= seat[row][left] + 1) {
          cur = min(cur, seat[row][left] + 1);
          break;
        }
      }
    }
    {
      ll up = row;
      while (up > 0) {
        up -= 1;
        if (seat[up][col] == -1) {
          continue;
        }
        if (cur >= seat[up][col] + 1) {
          cur = min(cur, seat[up][col] + 1);
          break;
        }
      }
    }
    {
      ll down = row;
      while (down < N - 1) {
        down += 1;
        if (seat[down][col] == -1) {
          continue;
        }
        if (cur >= seat[down][col] + 1) {
          cur = min(cur, seat[down][col] + 1);
          break;
        }
      }
    }
    ans += cur;
    {
      ll right = col;
      ll prev = seat[row][right];
      while (right < N - 1) {
        right += 1;
        if (prev >= seat[row][right]) {
          break;
        }
        prev = seat[row][right];
        seat[row][right] = max(seat[row][right] - 1LL, 0LL);
      }
    }
    {
      ll left = col;
      ll prev = seat[row][col];
      while (left > 0) {
        left -= 1;
        if (prev >= seat[row][left]) {
          break;
        }
        prev = seat[row][left];
        seat[row][left] = max(seat[row][left] - 1LL, 0LL);
      }
    }
    {
      ll up = row;
      ll prev = seat[row][col];
      while (up > 0) {
        up -= 1;
        if (prev >= seat[up][col]) {
          break;
        }
        prev = seat[up][col];
        seat[up][col] = max(seat[up][col] - 1LL, 0LL);
      }
    }
    {
      ll down = row;
      ll prev = seat[row][col];
      while (down < N - 1) {
        down += 1;
        if (prev >= seat[down][col]) {
          break;
        }
        prev = seat[down][col];
        seat[down][col] = max(seat[down][col] - 1LL, 0LL);
      }
    }
    seat[row][col] = -1;
  }
  cout << ans << endl;
}
