#include <bits/stdc++.h>
using namespace std;
int b[250001], n, c[250001], used[250001];
vector<int> a[250001];
int main() {
  cin >> n;
  int nn = pow(n, 2);
  for (int i = 1; i <= nn; i++) {
    if (i >= n + 1) a[i].push_back(i - n);
    if (i + n <= nn) a[i].push_back(i + n);
    if ((i - 1) % n) a[i].push_back(i - 1);
    if (i % n) a[i].push_back(i + 1);
    int u = (i - 1) % n, v = (i - 1) / n;
    c[i] = min(min(u, n - 1 - u), min(v, n - 1 - v));
  }
  int ans = 0;
  for (int i = 1; i <= nn; i++) {
    int y;
    cin >> y;
    ans += c[y];
    used[y] = 1;
    if (y >= n + 1 && !used[y - n]) {
      if ((y - 1) % n && !used[y - 1]) a[y - n].push_back(y - 1);
      if (y % n && !used[y + 1]) a[y - n].push_back(y + 1);
      if (y + n <= nn && !used[y + n]) a[y - n].push_back(y + n);
    }
    if (y + n <= nn && !used[y + n]) {
      if ((y - 1) % n && !used[y - 1]) a[y + n].push_back(y - 1);
      if (y % n && !used[y + 1]) a[y + n].push_back(y + 1);
      if (y >= n + 1 && !used[y - n]) a[y + n].push_back(y - n);
    }
    if ((y - 1) % n && !used[y - 1]) {
      if (y >= n + 1 && !used[y - n]) a[y - 1].push_back(y - n);
      if (y + n <= nn && !used[y + n]) a[y - 1].push_back(y + n);
      if (y % n && !used[y + 1]) a[y - 1].push_back(y + 1);
    }
    if (y % n && !used[y + 1]) {
      if (y >= n + 1 && !used[y - n]) a[y + 1].push_back(y - n);
      if (y + n <= nn && !used[y + n]) a[y + 1].push_back(y + n);
      if ((y - 1) % n && !used[y - 1]) a[y + 1].push_back(y - 1);
    }
    queue<int> q;
    q.push(y);
    while (q.size()) {
      int x = q.front();
      q.pop();
      if (used[x])
        for (int i = 1; i <= a[x].size(); i++) {
          if (!used[a[x][i - 1]] && c[a[x][i - 1]] > c[x]) {
            c[a[x][i - 1]] = c[x];
            q.push(a[x][i - 1]);
          }
        }
      else
        for (int i = 1; i <= a[x].size(); i++) {
          if (!used[a[x][i - 1]] && c[a[x][i - 1]] > c[x] + 1) {
            c[a[x][i - 1]] = c[x] + 1;
            q.push(a[x][i - 1]);
          }
        }
    }
  }
  cout << ans;
  return 0;
}
