#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int v[505][505] = {0};
  int h[505][505] = {0};
  int len = n + 2;
  for (int i = 0; i < len; i++)
    for (int j = 0; j < len; j++) {
      v[i][j] = min(min(i, len - 1 - i), min(j, len - 1 - j)) - 1;
      h[i][j] = 1;
    }
  int ans = 0;
  int p;
  int diff[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  for (int k = 0; k < n * n; k++) {
    cin >> p;
    int i = (p - 1) / n + 1;
    int j = (p - 1) % n + 1;
    ans += v[i][j];
    map<pair<int, int>, bool> vis;
    queue<pair<int, int>> data;
    data.push({i, j});
    vis[{i, j}] = true;
    h[i][j] = 0;
    while (data.size() > 0) {
      auto val = data.front();
      data.pop();
      v[val.first][val.second]--;
      for (int k = 0; k < 4; k++) {
        int x = val.first + diff[k][0];
        int y = val.second + diff[k][1];
        if (v[x][y] - 1 - h[x][y] == v[val.first][val.second] && !vis[{x, y}]) {
          vis[{x, y}] = true;
          data.push({x, y});
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  long long t = 1;
  while (t > 0) {
    t--;
    solve();
  }
  return 0;
}
