#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int n;
  cin >> n;
  vector<vector<bool>> oc(n, vector<bool>(n, 1));
  long long ans = 0;
  for (int tt = 0; tt < (int)n * n; tt++) {
    int x;
    cin >> x;
    x--;
    int i = x / n, j = x % n;
    oc[i][j] = 0;
    vector<vector<int>> dist(n, vector<int>(n, 250000));
    vector<vector<bool>> done(n, vector<bool>(n));
    deque<pair<int, int>> q;
    q.push_back({i, j});
    dist[i][j] = 0;
    while (!q.empty()) {
      i = q.front().first;
      j = q.front().second;
      q.pop_front();
      if (done[i][j]) continue;
      done[i][j] = 1;
      for (int d = 0; d < (int)4; d++) {
        int x = i + dx[d], y = j + dy[d];
        if (0 <= x && x < n && 0 <= y && y < n)
          if (oc[x][y] && dist[x][y] > dist[i][j] + 1) {
            dist[x][y] = dist[i][j] + 1;
            q.push_back({x, y});
          } else if (!oc[x][y] && dist[x][y] > dist[i][j]) {
            dist[x][y] = dist[i][j];
            q.push_front({x, y});
          }
      }
    }
    int cur = 250000;
    for (int i = 0; i < (int)n; i++) {
      cur = min(cur, dist[0][i]);
      cur = min(cur, dist[i][0]);
      cur = min(cur, dist[n - 1][i]);
      cur = min(cur, dist[i][n - 1]);
    }
    ans += cur;
  }
  cout << ans << "\n";
}
