#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int& i : p) cin >> i;
  vector<vector<int> > _mp(n, vector<int>(n));
  for (int i = 0; i < n * n; ++i) {
    int c = p[i] - 1;
    _mp[c / n][c % n] = i;
  }
  const vector<vector<int> > mp = _mp;
  vector<vector<int> > ans(n, vector<int>(n, INT_MAX));
  deque<pair<int, int> > bfs;
  auto push = [&](pair<int, int> pos, int len, bool back) {
    if (ans[pos.first][pos.second] > len) {
      if (back)
        bfs.push_back(pos);
      else
        bfs.push_front(pos);
      ans[pos.first][pos.second] = len;
    }
  };
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j <= n - 1; j++) {
      int a = min(i, j);
      int na = min(n - 1 - i, n - 1 - j);
      ans[i][j] = min(a, na) + 1;
    }
  }
  int res = 0;
  vector<vector<int> > dirs({{0, 1}, {0, -1}, {-1, 0}, {1, 0}});
  for (int i = 0; i < n * n; ++i) {
    int c = p[i] - 1;
    res += ans[c / n][c % n] - 1;
    ans[c / n][c % n]--;
    queue<pair<int, int> > bfs;
    bfs.push({c / n, c % n});
    int cur = mp[c / n][c % n];
    while (!bfs.empty()) {
      pair<int, int> pos = bfs.front();
      bfs.pop();
      for (vector<int> dir : dirs) {
        int nx = pos.first + dir[0];
        int ny = pos.second + dir[1];
        int len = ans[pos.first][pos.second];
        if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
          if (mp[nx][ny] < cur) {
            if (ans[nx][ny] > len) {
              ans[nx][ny] = len;
              bfs.push({nx, ny});
            }
          } else {
            if (ans[nx][ny] > len + 1) {
              ans[nx][ny] = len + 1;
              bfs.push({nx, ny});
            }
          }
        }
      }
    }
  }
  cout << res;
}
