#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500;
struct Node {
  int y;
  int x;
  int dist;
  Node(int y = -1, int x = -1, int dist = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
  }
};
const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};
int grid[MAX_N][MAX_N];
bool checked[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int N;
void dfs(int y, int x, int dist) {
  if (grid[y][x] <= dist) return;
  if (visited[y][x]) return;
  visited[y][x] = true;
  grid[y][x] = max(0, min(grid[y][x], dist));
  for (int i = 0; i < 4; ++i) {
    int ny = y + DY[i];
    int nx = x + DX[i];
    if (ny < 0 || nx < 0 || N <= ny || N <= nx) continue;
    if (checked[ny][nx]) {
      dfs(ny, nx, dist);
    } else {
      dfs(ny, nx, dist + 1);
    }
  }
}
int main() {
  cin >> N;
  vector<int> P;
  for (int i = 0; i < N * N; ++i) {
    int p;
    cin >> p;
    P.push_back(p);
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      int my = min(y, N - y - 1);
      int mx = min(x, N - x - 1);
      grid[y][x] = min(my, mx);
    }
  }
  int ans = 0;
  memset(checked, false, sizeof(checked));
  for (int i = 0; i < N * N; ++i) {
    int p = P[i];
    int y = (p - 1) / N;
    int x = (p - 1) % N;
    int v = grid[y][x];
    checked[y][x] = true;
    ans += v;
    memset(visited, false, sizeof(visited));
    dfs(y, x, v - 1);
  }
  cout << ans << endl;
  return 0;
}
