#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int n;
int cost[250000], used[250000];
int dh[4] = {1, -1, 0, 0}, dw[4] = {0, 0, 1, -1};
void bfs(int src) {
  deque<int> dq;
  dq.push_back(src);
  while (!dq.empty()) {
    int cur = dq.front();
    int h = cur / n, w = cur % n;
    dq.pop_front();
    for (int i = 0; i < (4); ++i) {
      int nh = h + dh[i];
      int nw = w + dw[i];
      if (nh < 0 || n <= nh) continue;
      if (nw < 0 || n <= nw) continue;
      int nex = nh * n + nw;
      int nex_cost = cost[cur] + used[cur];
      if (cost[nex] <= nex_cost) continue;
      cost[nex] = nex_cost;
      if (used[cur])
        dq.push_back(nex);
      else
        dq.push_front(nex);
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  int m = n * n;
  for (int i = 0; i < (m); ++i)
    cost[i] = min({i / n, i % n, n - 1 - i / n, n - 1 - i % n});
  long long int ans = 0;
  memset(used, 1, sizeof(used));
  for (int i = 0; i < (m); ++i) {
    int p;
    cin >> p;
    --p;
    ans += cost[p];
    used[p] = 0;
    bfs(p);
  }
  cout << ans << endl;
  return 0;
}
