#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
vector<pair<int, int>> order;
vector<vector<int>> dist;
vector<vector<bool>> seats;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int n;
void updDist(int x1, int y1) {
  vector<pair<int, int>> q;
  q.push_back({x1, y1});
  for (int i = 0; i < q.size(); i++) {
    auto u = q[i];
    int x = u.first;
    int y = u.second;
    for (int d = 0; d < 4; d++) {
      int x2, y2;
      x2 = x + dx[d];
      y2 = y + dy[d];
      if (!(x2 < 0 || x2 > n - 1 || y2 < 0 || y2 > n - 1)) {
        int newDist = dist[x][y] + seats[x2][y2];
        if (newDist < dist[x2][y2]) {
          dist[x2][y2] = newDist;
          q.push_back({x2, y2});
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  order.resize(n * n);
  seats.assign(n, vector<bool>(n, 1));
  dist.assign(n, vector<int>(n, INF));
  for (auto &it : order) {
    int a;
    cin >> a;
    a--;
    int x = a / n;
    int y = a % n;
    it.first = x;
    it.second = y;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((i == 0 || i == n - 1 || j == 0 || j == n - 1)) {
        dist[i][j] = 0;
        updDist(i, j);
      }
    }
  }
  int ans = 0;
  for (auto u : order) {
    ans += dist[u.first][u.second];
    dist[u.first][u.second]--;
    seats[u.first][u.second] = 0;
    updDist(u.first, u.second);
  }
  cout << ans << '\n';
  return 0;
}
