#include <bits/stdc++.h>
using namespace std;
vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {-1, 1, 0, 0};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> best(n, vector<int>(n, n));
  vector<vector<int>> used(n, vector<int>(n, 1));
  function<void(int, int, int)> update = [&](int i, int j, int x) {
    if (x >= best[i][j]) return;
    best[i][j] = x;
    for (int k = 0; k < 4; ++k) {
      int nx = i + dx[k], ny = j + dy[k];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        update(nx, ny, best[i][j] + used[nx][ny]);
      }
    }
  };
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
        update(i, j, 1);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int x = p[i] / n, y = p[i] % n;
    ans += best[x][y] - 1;
    used[x][y] = 0;
    update(x, y, best[x][y] - 1);
  }
  cout << ans << '\n';
}
