#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct State {
  int x, y;
};
int main() {
  int N;
  cin >> N;
  vector<vector<bool>> out(N, vector<bool>(N, false));
  int ans = 0;
  for (int i = 0; i < N * N; ++i) {
    int P;
    cin >> P;
    --P;
    int sx = P % N;
    int sy = P / N;
    out[sy][sx] = true;
    deque<State> que;
    que.push_back({sx, sy});
    vector<vector<int>> step(N, vector<int>(N, -1));
    step[sy][sx] = 0;
    bool end = false;
    while (!que.empty() and !end) {
      assert((int)que.size() < N * N * N);
      State s = que.front();
      que.pop_front();
      for (int d = 0; d < 4; ++d) {
        int nx = s.x + dx[d], ny = s.y + dy[d];
        if (nx < 0 or nx >= N or ny < 0 or ny >= N) {
          ans += step[s.y][s.x];
          end = true;
          break;
        } else {
          if (step[ny][nx] != -1) continue;
          if (out[ny][nx]) {
            step[ny][nx] = step[s.y][s.x];
            que.push_front({nx, ny});
          } else {
            step[ny][nx] = step[s.y][s.x] + 1;
            que.push_back({nx, ny});
          }
        }
      }
    }
  }
  cout << ans << endl;
}
