#include <bits/stdc++.h>
using namespace std;
struct uset {
  vector<int> fa;
  vector<int> val;
  uset(int n) {
    fa.resize(n);
    val.resize(n);
    fill(val.begin(), val.end(), 0x7fffffff);
    for (int i = 0; i < n; ++i) fa[i] = i;
  }
  int getfa(int a) { return fa[a] == a ? a : (fa[a] = getfa(fa[a])); }
  int &getval(int a) { return val[getfa(a)]; }
  void merge(int a, int b) {
    if (getfa(a) == getfa(b)) return;
    if (getval(a) < getval(b))
      fa[getfa(b)] = getfa(a);
    else
      fa[getfa(a)] = getfa(b);
  }
};
int n;
int seq[500];
bool gone[500][500];
uset us(500 * 500 + 1);
int tonum(int x, int y) { return x * n + y; }
void update(int x, int y) {
  gone[x][y] = true;
  if (x != 0 && gone[x - 1][y]) us.merge(tonum(x, y), tonum(x - 1, y));
  if (x != n - 1 && gone[x + 1][y]) us.merge(tonum(x, y), tonum(x + 1, y));
  if (y != 0 && gone[x][y - 1]) us.merge(tonum(x, y), tonum(x, y - 1));
  if (y != n - 1 && gone[x][y + 1]) us.merge(tonum(x, y), tonum(x, y + 1));
}
int mget(int x, int y) {
  if (x == 0 || x == n - 1 || y == 0 || y == n - 1) return 0;
  int ans = 9999;
  if (x != 0)
    ans = min(ans, us.getval(tonum(x - 1, y)) + (!gone[x - 1][y] ? 1 : 0));
  if (x != n - 1)
    ans = min(ans, us.getval(tonum(x + 1, y)) + (!gone[x + 1][y] ? 1 : 0));
  if (y != 0)
    ans = min(ans, us.getval(tonum(x, y - 1)) + (!gone[x][y - 1] ? 1 : 0));
  if (y != n - 1)
    ans = min(ans, us.getval(tonum(x, y + 1)) + (!gone[x][y + 1] ? 1 : 0));
  return ans;
}
void init() {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      us.getval(tonum(i, j)) = min({i, n - 1 - i, j, n - 1 - j});
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; ++i) {
    cin >> seq[i];
    --seq[i];
  }
  init();
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    ans += mget(seq[i] / n, seq[i] % n);
    update(seq[i] / n, seq[i] % n);
  }
  cout << ans;
  return 0;
}
