#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};
using pii = pair<int, int>;
struct State {
  int x, y, c;
};
int main() {
  int N;
  cin >> N;
  vector<vector<bool>> out(N, vector<bool>(N, false));
  vector<vector<int>> vis(N, vector<int>(N, -1));
  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, 0});
    bool end = false;
    while (!que.empty() and !end) {
      auto [x, y, c] = que.front();
      que.pop_front();
      for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 or nx >= N or ny < 0 or ny >= N) {
          ans += c;
          end = true;
          break;
        }
        if (vis[ny][nx] != P) {
          vis[ny][nx] = P;
          if (out[ny][nx])
            que.push_front({nx, ny, c});
          else
            que.push_back({nx, ny, c + 1});
        }
      }
    }
  }
  cout << ans << endl;
}
