#include <bits/stdc++.h>
using namespace std;
void updateCost(int n, int *cost, int *costzero, int *tgts, int *exist,
                int &costzero_len, int costzero_ofs) {
  int i;
  int tgt_len = 0;
  for (i = costzero_ofs; i < costzero_len; i++) {
    if ((costzero[i] > n) && (cost[costzero[i] - 1] > exist[costzero[i]])) {
      tgts[tgt_len] = costzero[i] - 1;
      cost[costzero[i] - 1] = exist[costzero[i]];
      tgt_len++;
      if (exist[costzero[i]] == 0) {
        costzero[costzero_len] = costzero[i] - 1;
        costzero_len++;
      }
    }
    if ((costzero[i] < n * n - n) &&
        (cost[costzero[i] + 1] > exist[costzero[i]])) {
      tgts[tgt_len] = costzero[i] + 1;
      cost[costzero[i] + 1] = exist[costzero[i]];
      tgt_len++;
      if (exist[costzero[i]] == 0) {
        costzero[costzero_len] = costzero[i] + 1;
        costzero_len++;
      }
    }
    if ((costzero[i] > n) && (cost[costzero[i] - n] > exist[costzero[i]])) {
      tgts[tgt_len] = costzero[i] - n;
      cost[costzero[i] - n] = exist[costzero[i]];
      tgt_len++;
      if (exist[costzero[i]] == 0) {
        costzero[costzero_len] = costzero[i] - n;
        costzero_len++;
      }
    }
    if ((costzero[i] < n * n - n) &&
        (cost[costzero[i] + n] > exist[costzero[i]])) {
      tgts[tgt_len] = costzero[i] + n;
      cost[costzero[i] + n] = exist[costzero[i]];
      tgt_len++;
      if (exist[costzero[i]] == 0) {
        costzero[costzero_len] = costzero[i] + n;
        costzero_len++;
      }
    }
  }
  for (i = 0; i < tgt_len; i++) {
    if ((tgts[i] > n) && (cost[tgts[i] - 1] > cost[tgts[i]] + exist[tgts[i]])) {
      tgts[tgt_len] = tgts[i] - 1;
      cost[tgts[i] - 1] = cost[tgts[i]] + exist[tgts[i]];
      tgt_len++;
    }
    if ((tgts[i] < n * n - 1) &&
        (cost[tgts[i] + 1] > cost[tgts[i]] + exist[tgts[i]])) {
      tgts[tgt_len] = tgts[i] + 1;
      cost[tgts[i] + 1] = cost[tgts[i]] + exist[tgts[i]];
      tgt_len++;
    }
    if ((tgts[i] > n) && (cost[tgts[i] - n] > cost[tgts[i]] + exist[tgts[i]])) {
      tgts[tgt_len] = tgts[i] - n;
      cost[tgts[i] - n] = cost[tgts[i]] + exist[tgts[i]];
      tgt_len++;
    }
    if ((tgts[i] < n * n - 1) &&
        (cost[tgts[i] + n] > cost[tgts[i]] + exist[tgts[i]])) {
      tgts[tgt_len] = tgts[i] + n;
      cost[tgts[i] + n] = cost[tgts[i]] + exist[tgts[i]];
      tgt_len++;
    }
  }
}
int main() {
  int n;
  cin >> n;
  int cost[n * n], costzero[n * n], tgts[n * n], exists[n * n];
  int i, j, k;
  int costzero_len = 0, costzero_ofs = 0;
  ;
  int p;
  for (i = 0; i < n * n; i++) {
    cost[i] = n * n;
    costzero[i] = -1;
    exists[i] = 1;
  }
  for (i = 0; i < n; i++) {
    costzero[costzero_len] = i;
    costzero[costzero_len + 1] = n * n - n + i;
    cost[i] = 0;
    cost[n * n - n + i] = 0;
    costzero_len += 2;
  }
  for (i = 2; i < n - 1; i++) {
    costzero[costzero_len] = i * n - 1;
    costzero[costzero_len + 1] = i * n;
    cost[i * n - 1] = 0;
    cost[i * n] = 0;
    costzero_len += 2;
  }
  costzero[costzero_len] = n;
  costzero[costzero_len + 1] = n * n - n - 1;
  cost[n] = 0;
  cost[n * n - n - 1] = 0;
  costzero_len += 2;
  updateCost(n, cost, costzero, tgts, exists, costzero_len, costzero_ofs);
  long ans = 0;
  for (i = 0; i < n * n; i++) {
    cin >> p;
    p -= 1;
    if (cost[p] == 0) {
      if ((p > n) && (cost[p - 1] > 0)) {
        cost[p - 1] = 0;
        costzero[costzero_len] = p - 1;
        costzero_len++;
      }
      if ((p < n * n - n) && (cost[p + 1] > 0)) {
        cost[p + 1] = 0;
        costzero[costzero_len] = p + 1;
        costzero_len++;
      }
      if ((p > n) && (cost[p - n] > 0)) {
        cost[p - n] = 0;
        costzero[costzero_len] = p - n;
        costzero_len++;
      }
      if ((p < n * n - n) && (cost[p + n] > 0)) {
        cost[p + n] = 0;
        costzero[costzero_len] = p + n;
        costzero_len++;
      }
      for (j = costzero_ofs; j < costzero_len; j++) {
        if (costzero[j] == p) {
          costzero[j] = costzero[costzero_ofs];
          costzero_ofs++;
          break;
        }
      }
    } else {
      updateCost(n, cost, costzero, tgts, exists, costzero_len, costzero_ofs);
      ans += cost[p];
    }
    exists[p] = 0;
  }
  cout << ans << endl;
  return 0;
}
