#include <bits/stdc++.h>
using namespace std;
vector<vector<long>> grid;
vector<vector<bool>> occupied;
void dfs(long row, long col, long val) {
  if (!occupied[row][col] && grid[row][col] > val) {
    grid[row][col] = val;
    dfs(row + 1, col, val);
    dfs(row - 1, col, val);
    dfs(row, col + 1, val);
    dfs(row, col - 1, val);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long n;
  cin >> n;
  grid.resize(n + 2, vector<long>(n + 2, 0));
  occupied.resize(n + 2, vector<bool>(n + 2, false));
  for (long i = 0; i < grid.size(); i++) {
    for (long j = 0; j < grid.size(); j++) {
      grid[i][j] = min(
          min(i, j), min((long)grid.size() - i - 1, (long)grid.size() - j - 1));
      occupied[i][j] = (grid[i][j] != 0);
    }
  }
  long ans = 0;
  for (long i = 0; i < n * n; i++) {
    long k;
    cin >> k;
    long row = (k - 1) / n + 1;
    long col = (k - 1) % n + 1;
    long x = min(min(grid[row - 1][col], grid[row + 1][col]),
                 min(grid[row][col + 1], grid[row][col - 1]));
    ans += x;
    occupied[row][col] = false;
    dfs(row, col, x);
  }
  cout << ans << "\n";
  return 0;
}
