#include <bits/stdc++.h>
using namespace std;
int func(int** field, int r, int c, int n);
int main() {
  int n;
  cin >> n;
  int** field = new int*[n];
  for (int i = 0; i < n; i++) {
    field[i] = new int[n];
    for (int j = 0; j < n; j++) {
      field[i][j] = 1;
    }
  }
  pair<int, int>* p = new pair<int, int>[n * n];
  int tmp;
  for (int i = 0; i < n * n; i++) {
    cin >> tmp;
    tmp--;
    p[i] = make_pair(tmp / n, tmp % n);
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    ans += func(field, p[i].first, p[i].second, n);
    field[p[i].first][p[i].second] = 0;
  }
  cout << ans << endl;
}
int func(int** field, int r, int c, int n) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pair<int, pair<int, int>> now;
  pq.push(make_pair(0, make_pair(r, c)));
  bool** arl = new bool*[n];
  for (int i = 0; i < n; i++) {
    arl[i] = new bool[n];
    for (int j = 0; j < n; j++) {
      arl[i][j] = false;
    }
  }
  arl[r][c] = true;
  while (1) {
    now = pq.top();
    pq.pop();
    if (now.second.first == 0 || now.second.first == n - 1 ||
        now.second.second == 0 || now.second.second == n - 1) {
      return now.first;
    }
    if (!arl[now.second.first - 1][now.second.second]) {
      pq.push(
          make_pair(now.first + field[now.second.first - 1][now.second.second],
                    make_pair(now.second.first - 1, now.second.second)));
      arl[now.second.first - 1][now.second.second] = true;
    }
    if (!arl[now.second.first + 1][now.second.second]) {
      pq.push(
          make_pair(now.first + field[now.second.first + 1][now.second.second],
                    make_pair(now.second.first + 1, now.second.second)));
      arl[now.second.first + 1][now.second.second] = true;
    }
    if (!arl[now.second.first][now.second.second - 1]) {
      pq.push(
          make_pair(now.first + field[now.second.first][now.second.second - 1],
                    make_pair(now.second.first, now.second.second - 1)));
      arl[now.second.first][now.second.second - 1] = true;
    }
    if (!arl[now.second.first][now.second.second + 1]) {
      pq.push(
          make_pair(now.first + field[now.second.first][now.second.second + 1],
                    make_pair(now.second.first, now.second.second + 1)));
      arl[now.second.first][now.second.second + 1] = true;
    }
  }
}
