#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> P) {
  out << '(' << P.first << ',' << P.second << ')';
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> V) {
  for (int i = 0; i < ((int)((int)V.size())); i++)
    out << V[i] << ((i != ((int)V.size()) - 1) ? " " : "");
  return out;
}
const long long maxn = 505;
const long long maxlg = 20;
const long long INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const long long MOD = long long(1e9 + 7);
const long double PI = acos(-1);
const long double eps = 1e-9;
long long mypow(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
int n;
int p[maxn * maxn];
int d[maxn][maxn];
bool vis[maxn][maxn];
vector<pair<int, int> > q;
bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }
long long sumD;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < ((int)n); i++)
    for (int j = 0; j < ((int)n); j++) {
      d[i][j] = min({i + 1, n - i, j + 1, n - j});
      sumD += d[i][j];
    }
  for (int i = 0; i < ((int)n); i++)
    for (int j = 0; j < ((int)n); j++) vis[i][j] = 1;
  int ans = 0;
  for (int I = 0; I < ((int)n * n); I++) {
    int u, v;
    {
      int tmp;
      cin >> tmp;
      tmp--;
      u = tmp / n;
      v = tmp % n;
    }
    ans += d[u][v];
    d[u][v]--;
    vis[u][v] = 0;
    int pt = 0;
    q.emplace_back(u, v);
    while (pt < ((int)q.size())) {
      int x = q[pt].first, y = q[pt].second;
      pt++;
      {
        int _x = x + 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.emplace_back(_x, _y);
        }
      }
      {
        int _x = x - 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.emplace_back(_x, _y);
        }
      }
      {
        int _x = x, _y = y + 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.emplace_back(_x, _y);
        }
      }
      {
        int _x = x, _y = y - 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.emplace_back(_x, _y);
        }
      }
    }
    long long s = 0;
    for (int i = 0; i < ((int)n); i++)
      for (int j = 0; j < ((int)n); j++) s += d[i][j];
    assert(((int)q.size()) <= sumD - s);
    sumD = s;
    q.clear();
  }
  cout << ans - n * n << '\n';
  return 0;
}
