#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll NMAX = 1e4;
ll dp[NMAX];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
const int INF = 1e8;
int main() {
  int n;
  cin >> n;
  vector<vector<bool>> s(n, vector<bool>(n, true));
  int hate = 0;
  for (int i = 0; i < (n * n); ++i) {
    vector<vector<bool>> ac(n, vector<bool>(n, false));
    vector<vector<int>> cf(n, vector<int>(n, INF));
    queue<P> q, q_next;
    int p;
    cin >> p;
    --p;
    int x = p % n, y = p / n;
    s[x][y] = false;
    if (x == 0 || y == 0 || x == n - 1 || y == n - 1) {
      continue;
    }
    ac[x][y] = true;
    q_next.push({x, y});
    bool judge = true;
    int j = 0;
    while (judge) {
      queue<P> q_void;
      if (q_next.size() == 0) {
        break;
      }
      swap(q, q_next);
      swap(q_next, q_void);
      while (!q.empty()) {
        P q1 = q.front();
        q.pop();
        int x0 = q1.first;
        int y0 = q1.second;
        if (x0 == 0 || x0 == n - 1 || y0 == 0 || y0 == n - 1) {
          hate += j;
          judge = false;
          break;
        }
        for (int k = 0; k < (4); ++k) {
          int x1 = x0 + dx[k];
          int y1 = y0 + dy[k];
          if (ac[x1][y1] == false) {
            ac[x1][y1] = true;
            if (s[x1][y1] == true) {
              q_next.push({x1, y1});
            } else {
              q.push({x1, y1});
            }
          }
        }
      }
      j++;
    }
  }
  cout << hate << endl;
}
