#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool check[510][510];
int cnt[510][510];
int n;
bool check_rec[510][510];
void rec(int w, int h) {
  if (w > 1) {
    if (cnt[h][w - 1] > cnt[h][w] + 1) {
      cnt[h][w - 1] = cnt[h][w] + 1;
      if (!check[h][w - 1]) cnt[h][w - 1]--;
      rec(w - 1, h);
    } else if (cnt[h][w - 1] == cnt[h][w] + 1 && !check[h][w - 1]) {
      cnt[h][w - 1]--;
      rec(w - 1, h);
    }
  }
  if (h > 1) {
    if (cnt[h - 1][w] > cnt[h][w] + 1) {
      cnt[h - 1][w] = cnt[h][w] + 1;
      if (!check[h - 1][w]) cnt[h - 1][w]--;
      rec(w, h - 1);
    } else if (cnt[h - 1][w] == cnt[h][w] + 1 && !check[h - 1][w]) {
      cnt[h - 1][w]--;
      rec(w, h - 1);
    }
  }
  if (w < n + 1) {
    if (cnt[h][w + 1] > cnt[h][w] + 1) {
      cnt[h][w + 1] = cnt[h][w] + 1;
      if (!check[h][w + 1]) cnt[h][w + 1]--;
      rec(w + 1, h);
    } else if (cnt[h][w + 1] == cnt[h][w] + 1 && !check[h][w + 1]) {
      cnt[h][w + 1]--;
      rec(w + 11, h);
    }
  }
  if (h < n + 1) {
    if (cnt[h + 1][w] > cnt[h][w] + 1) {
      cnt[h + 1][w] = cnt[h][w] + 1;
      if (!check[h + 1][w]) cnt[h + 1][w]--;
      rec(w, h + 1);
    } else if (cnt[h + 1][w] == cnt[h][w] + 1 && !check[h + 1][w]) {
      cnt[h + 1][w]--;
      rec(w, h + 1);
    }
  }
}
int main() {
  cin >> n;
  int tmp;
  for (int(i) = (0); (i) < (510); (i)++) {
    for (int(j) = (0); (j) < (510); (j)++) {
      check[i][j] = true;
    }
  }
  int w, h;
  ll ans = 0;
  for (int(i) = (0); (i) < (n * n); (i)++) {
    int tmp2 = 1e9;
    cin >> tmp;
    h = ((tmp - 1) / n) + 1;
    w = ((tmp - 1) % n) + 1;
    check[h][w] = false;
    if (w == 1 || h == 1 || w == n || h == n) {
      continue;
    }
    for (int(i) = (0); (i) < (510); (i)++) {
      for (int(j) = (0); (j) < (510); (j)++) {
        cnt[i][j] = 1e9;
        check_rec[i][j] = false;
      }
    }
    cnt[h][w] = 0;
    rec(w, h);
    for (int(i) = (1); (i) < (n + 1); (i)++) {
      tmp2 = min(tmp2, cnt[i][1]);
      tmp2 = min(tmp2, cnt[i][n]);
      tmp2 = min(tmp2, cnt[1][i]);
      tmp2 = min(tmp2, cnt[n][i]);
    }
    ans += tmp2;
  }
  cout << ans << endl;
  return 0;
}
