#include <bits/stdc++.h>
using namespace std;
const int DIM = 501;
const int INF = 1e9;
int n;
int x[] = {1, 0, -1, 0};
int y[] = {0, 1, 0, -1};
int isin(int l, int r) {
  if (l <= n && r <= n && l >= 1 && r >= 1)
    return 1;
  else
    return 0;
}
int v[DIM][DIM];
int p[DIM * DIM];
bool viz[DIM][DIM];
struct compara {
  bool operator()(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
    return a.first > b.first;
  }
};
int dfs(int l, int r) {
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >, compara>
      q;
  map<pair<int, pair<int, int> >, bool> ex;
  int ex2[DIM][DIM];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) ex2[i][j] = INF;
  q.push({0, {l, r}});
  int dist = min(min(min(l - 1, n - l), r - 1), n - r);
  int ans = INF;
  while (!q.empty()) {
    if (q.top().first > ans) return ans;
    int nr = q.top().first;
    int l1 = q.top().second.first;
    int r1 = q.top().second.second;
    q.pop();
    if ((l1 == 1) || (l1 == n) || (r1 == 1) || (r1 == n)) {
      ans = min(ans, nr);
      if (ans == 0) return 0;
    }
    for (int i = 0; i < 4; i++) {
      int l2 = l1 + x[i];
      int r2 = r1 + y[i];
      if (isin(l2, r2)) {
        if (viz[l2][r2] == false) {
          if (ex[{nr, {l2, r2}}] == false && nr < ex2[l2][r2]) {
            q.push({nr, {l2, r2}});
            ex[{nr, {l2, r2}}] = true;
            ex2[l2][r2] = nr;
          }
        } else if (nr + 1 <= dist)
          if (ex[{nr + 1, {l2, r2}}] == false && nr + 1 < ex2[l2][r2]) {
            q.push({nr + 1, {l2, r2}});
            ex[{nr + 1, {l2, r2}}] = true;
            ex2[l2][r2] = nr + 1;
          }
      }
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) v[i][j] = n * (i - 1) + j;
  for (int i = 1; i <= n * n; i++) cin >> p[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) viz[i][j] = true;
  int rez = 0;
  for (int i = 1; i <= n * n; i++) {
    int r = p[i] % n;
    int l = p[i] / n + 1;
    if (r == 0) {
      r = n;
      l--;
    }
    rez += dfs(l, r);
    viz[l][r] = false;
  }
  cout << rez << '\n';
  return 0;
}
