#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int mod = 1e9 + 7;
const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
void YesNo(bool j) {
  cout << (j ? "Yes" : "No") << endl;
  return;
}
void yesno(bool j) {
  cout << (j ? "yes" : "no") << endl;
  return;
}
template <class Head>
void pt(Head&& head) {
  cout << head << endl;
  return;
}
vector<int> to[505];
void dfs(int j, int dn, vector<int>& nuke) {
  nuke[j] -= dn;
  for (int u : to[j]) {
    dfs(u, dn, nuke);
  }
  return;
}
int main() {
  int n;
  cin >> n;
  int nn = n * n;
  vector<int> a(nn);
  for (int i = 0; i < (nn); ++i) {
    cin >> a[i];
  }
  int ans = 0;
  vector<int> nuke(nn + 1, inf);
  vector<int> oyas(nn + 1, 0);
  for (int i = 0; i < (nn); ++i) {
    int x = a[i] / n;
    int y = a[i] % n;
    if (y == 0) y = n;
    x++;
    int dist = min(min(x - 1, n - x), min(y - 1, n - y));
    int oya = 0;
    for (int j = 0; j < (nn + 1); ++j) {
      if (j == 0) continue;
      if (nuke[j] == inf) continue;
      int b = j;
      int xj = b / n;
      int yj = b % n;
      if (yj == 0) y = n;
      xj++;
      int ndist = max(x - xj, xj - x) + max(y - yj, yj - y) - 1 + nuke[j];
      pt(ndist);
      dist = min(dist, ndist);
      if (dist == ndist) oya = b;
    }
    ans += dist;
    nuke[a[i]] = dist;
    if (oya != 0) {
      to[oya].push_back(a[i]);
      oyas[a[i]]--;
    }
    for (int j = 0; j < (nn + 1); ++j) {
      if (j == 0) continue;
      if (nuke[j] == inf) continue;
      if (j == a[i]) continue;
      if (oyas[j] < 0) continue;
      int b = j;
      int xj = b / n;
      int yj = b % n;
      if (yj == 0) y = n;
      xj++;
      int ndist = max(x - xj, xj - x) + max(y - yj, yj - y) - 1 + dist;
      if (ndist < nuke[j]) {
        int dn = nuke[j] - ndist;
        dfs(j, dn, nuke);
      }
    }
    for (int j = 0; j < (nn + 1); ++j) {
      if (j == 0) continue;
      if (nuke[j] == inf) continue;
      if (j == a[i]) continue;
      if (oyas[j] >= 0) continue;
      int b = j;
      int xj = b / n;
      int yj = b % n;
      if (yj == 0) y = n;
      xj++;
      int ndist = max(x - xj, xj - x) + max(y - yj, yj - y) - 1 + dist;
      if (ndist < nuke[j]) {
        int dn = nuke[j] - ndist;
        dfs(j, dn, nuke);
      }
    }
  }
  pt(ans);
  return 0;
}
