#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
int sit[1502][1502];
int uf[1502][1502][2];
vector<int> parent(vector<int> c) {
  if (uf[c[0]][c[1]][0] < 0)
    return c;
  else {
    return vector<int>({uf[c[0]][c[1]][0], uf[c[0]][c[1]][1]}) =
               parent({uf[c[0]][c[1]][0], uf[c[0]][c[1]][1]});
  }
}
int get(vector<int> l) {
  vector<int> p = parent(l);
  return abs(uf[p[0]][p[1]][0]) - 1;
}
void merge(vector<int>& a, vector<int>& b) {
  if (get(b) > get(a)) swap(a, b);
  vector<int> pa = parent(a);
  vector<int> push_back = parent(b);
  if (pa[0] == push_back[0] && pa[1] == push_back[1]) return;
  uf[pa[0]][pa[1]][0] = push_back[0];
  uf[pa[0]][pa[1]][1] = push_back[1];
  uf[push_back[0]][push_back[1]][0] = -get(b) - 1;
  uf[push_back[0]][push_back[1]][0] = -get(b) - 1;
}
void solve() {
  int n;
  cin >> n;
  memset(sit, 0, sizeof sit);
  memset(uf, -1, sizeof uf);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      sit[i][j] = 1000;
    }
  }
  int ans = 0;
  for (int(ii) = 0; (ii) < (n * n); (ii)++) {
    int x;
    cin >> x;
    int r = (x - 1) / n + 1;
    int c = (x % n - 1 + n) % n + 1;
    int right = 1000, down = 1000, up = 1000, left = 1000;
    for (int i = 0; i < 1000; i++) {
      if (sit[r + i][c] != 1000) {
        right = get({r + i, c}) + i - 1;
        break;
      }
    }
    for (int i = 0; i < 1000; i++) {
      if (sit[r - i][c] != 1000) {
        left = get({r - i, c}) + i - 1;
        break;
      }
    }
    for (int i = 0; i < 1000; i++) {
      if (sit[r][c + i] != 1000) {
        down = get({r, c + i}) + i - 1;
        break;
      }
    }
    for (int i = 0; i < 1000; i++) {
      if (sit[r][c - i] != 1000) {
        up = get({r, c - i}) + i - 1;
        break;
      }
    }
    int mn = min(min(up, down), min(right, left));
    sit[r][c] = 0;
    uf[r][c][0] = -mn - 1;
    uf[r][c][1] = -mn - 1;
    if (sit[r - 1][c] != 1000) {
      vector<int> aa = {r - 1, c};
      vector<int> bb = {r, c};
      merge(aa, bb);
    }
    if (sit[r][c - 1] != 1000) {
      vector<int> aa = {r, c - 1};
      vector<int> bb = {r, c};
      merge(aa, bb);
    }
    if (sit[r + 1][c] != 1000) {
      vector<int> aa = {r + 1, c};
      vector<int> bb = {r, c};
      merge(aa, bb);
    }
    if (sit[r][c + 1] != 1000) {
      vector<int> aa = {r, c + 1};
      vector<int> bb = {r, c};
      merge(aa, bb);
    }
    ans += mn;
    for (int rr = 1; rr <= n; rr++) {
      for (int cc = 1; cc <= n; cc++) {
      }
    }
  }
  cout << ans;
}
int main() { solve(); }
