#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
using ll = long long;
const int INF = 1e8;
int main() {
  int ans = 0;
  int N;
  cin >> N;
  vector<vector<int>> finish_table(N, vector<int>(N, INF));
  vector<int> v(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> v[i];
  }
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  for (int i = 0; i < N * N; i++) {
    int tmp_ans = INF;
    queue<pair<int, int>> que;
    int sx = (v[i] - 1) / N;
    int sy = (v[i] - 1) % N;
    que.push(pair<int, int>(sx, sy));
    vector<vector<int>> d(N, vector<int>(N, INF));
    d[sx][sy] = 0;
    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      if (d[p.first][p.second] >= tmp_ans) {
        continue;
      }
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j];
        int ny = p.second + dy[j];
        if (nx < 0 || nx >= N || ny < 0 || ny >= N) {
          tmp_ans = min(d[p.first][p.second], tmp_ans);
          continue;
        }
        if (d[nx][ny] <= d[p.first][p.second]) {
          continue;
        }
        if (finish_table[nx][ny] == 0) {
          tmp_ans = min(d[p.first][p.second], tmp_ans);
          continue;
        }
        if (finish_table[nx][ny] == INF) {
          if (d[nx][ny] <= d[p.first][p.second] + 1) {
            continue;
          }
          d[nx][ny] = d[p.first][p.second] + 1;
        } else {
          d[nx][ny] = d[p.first][p.second];
        }
        que.push(pair<int, int>(nx, ny));
      }
    }
    finish_table[sx][sy] = tmp_ans;
    ans += tmp_ans;
  }
  cout << ans << endl;
  return 0;
}
