#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> dist;
vector<vector<int>> there;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n;
void upd (int x, int y, int d) {
  dist[x][y] = d;
  d += there[x][y];
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx < 0 || xx >= n || yy < 0 || yy >= n || dist[xx][yy] <= d) continue;
    upd(xx, yy, d);
  }
}
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  dist = vector<vector<int>>(n, vector<int>(n));
  there = vector<vector<int>>(n, vector<int>(n, 1));
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
    dist[i][j] = min({i, j, n - 1 - i, n - 1 - j});
  }
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
  }
  int ans = 0;
  for (auto& k : p) {
    int x = k / n;
    int y = k % n;
    ans += dist[x][y];
    there[x][y] = 0;
    upd(x, y, dist[x][y]);
  }
  cout << ans << endl;
  return 0;
}