#include <bits/stdc++.h>
using namespace std;
template <class T>
void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\n";
}
template <class T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, T b) {
  if (a < b) a = b;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> grid(n, vector<int>(n, n));
  {
    queue<pair<int, int>> q;
    for (int i = 0; i < n; i++) {
      grid[0][i] = 0;
      grid[i][0] = 0;
      grid[n - 1][i] = 0;
      grid[i][n - 1] = 0;
      q.push({0, i});
      q.push({i, 0});
      q.push({n - 1, i});
      q.push({i, n - 1});
    }
    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop();
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y] + 1) {
            chmin(grid[nx][ny], grid[x][y] + 1);
            q.push({nx, ny});
          }
        }
      }
    }
  }
  int ans = 0;
  vector<vector<bool>> empty(n, vector<bool>(n, false));
  for (int i = 0; i < n * n; i++) {
    int cur;
    cin >> cur;
    cur--;
    int cx = cur / n, cy = cur % n;
    ans += grid[cx][cy];
    empty[cx][cy] = true;
    queue<pair<int, int>> que;
    que.push({cx, cy});
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop();
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y]) {
            chmin(grid[nx][ny], grid[x][y] + (empty[x][y] ? 0 : 1));
            que.push({nx, ny});
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
