#include <bits/stdc++.h>
using namespace std;
void debugF(ostream &os) { os << endl; }
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) {
  os << " " << H;
  debugF(os, T...);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); os << (i > 0 ? " " : "") << v[i++])
    ;
  return os;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int i, j;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<int>> rem(n, vector<int>(n, 0));
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      int dd = min({i, n - 1 - i, j, n - 1 - j});
      dd++;
      dist[i][j] = dd;
    }
  }
  int ans = 0;
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  function<void(int, int)> dfs = [&](int x, int y) {
    for (j = 0; j < 4; j++) {
      int xx = x + dx[j];
      int yy = y + dy[j];
      if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
      if (dist[xx][yy] > dist[x][y] + (1 - rem[xx][yy])) {
        dist[xx][yy] = dist[x][y] + (1 - rem[xx][yy]);
        dfs(xx, yy);
      }
    }
  };
  for (i = 0; i < n * n; i++) {
    int pos;
    cin >> pos;
    pos--;
    int r, c;
    r = (pos) / n;
    c = (pos) % n;
    ans += dist[r][c] - 1;
    rem[r][c] = 1;
    dist[r][c]--;
    dfs(r, c);
  }
  cout << ans << "\n";
  return 0;
}
