#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long int LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI = acos(-1.0L);
__attribute__((constructor)) void initial() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
int n;
int maze[505][505];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs(int sx, int sy) {
  if (sx == 0 || sy == 0 || sy == n - 1 || sy == n - 1) return 0;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  int d[505][505];
  int ret = INF;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) d[i][j] = INF;
  q.push(make_pair(0, make_pair(sx, sy)));
  d[sx][sy] = 0;
  while (q.size()) {
    pair<int, pair<int, int>> u = q.top();
    q.pop();
    if (u.first >= ret) return ret;
    pair<int, int> p = u.second;
    if (p.first == n - 1 || p.first == 0 || p.second == n - 1 || p.second == 0)
      return u.first;
    for (int i = 0; i < (int)(4); i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        if (maze[nx][ny]) {
          if (d[nx][ny] > d[p.first][p.second] + 1) {
            d[nx][ny] = d[p.first][p.second] + 1;
            q.push(make_pair(d[nx][ny], make_pair(nx, ny)));
          }
        } else {
          if (d[nx][ny] > d[p.first][p.second]) {
            d[nx][ny] = d[p.first][p.second];
            q.push(make_pair(d[nx][ny], make_pair(nx, ny)));
          }
        }
      }
    }
  }
  for (int i = 0; i < (int)(n); i++) {
    ret = (((ret) > (d[i][0])) ? (d[i][0]) : (ret));
    ret = (((ret) > (d[i][n - 1])) ? (d[i][n - 1]) : (ret));
    ret = (((ret) > (d[0][i])) ? (d[0][i]) : (ret));
    ret = (((ret) > (d[n - 1][i])) ? (d[n - 1][i]) : (ret));
  }
  return ret;
}
signed main() {
  cin >> n;
  int ans = 0;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) maze[i][j] = 1;
  for (int i = 0; i < (int)(n * n); i++) {
    int p;
    cin >> p;
    p--;
    int sx = p / n, sy = p % n;
    ans += bfs(sx, sy);
    maze[sx][sy] = 0;
  }
  cout << ans << endl;
  return 0;
}
