#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  const int INF = INT32_MAX;
  vector<bool> k(n, true);
  vector<int> d(n * n, INF);
  for (int i = 0; i < (n * n); ++i) {
    int x = i % n;
    int y = i / n;
    chmin(d[i], x);
    chmin(d[i], y);
    chmin(d[i], n - x - 1);
    chmin(d[i], n - y - 1);
  }
  int ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    int p;
    cin >> p;
    p--;
    ans += d[p];
    k[p] = false;
    queue<int> q;
    q.emplace(p);
    while (!q.empty()) {
      int nowp = q.front();
      q.pop();
      int x = nowp % n;
      int y = nowp / n;
      for (int j = 0; j < (4); ++j) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        int np = ny * n + nx;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
        if (d[np] > d[nowp] + k[nowp]) {
          d[np] = d[nowp] + k[nowp];
          q.emplace(np);
        }
      }
    }
  }
  cout << ans << endl;
}
