#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
struct pnt {
  int y, x, cost;
};
vector<vector<int>> mv = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int N;
int bfs(int y, int x, vector<vector<int>> &grid, vector<vector<int>> &attend) {
  queue<pnt> que;
  attend[y][x] = 0;
  que.push({y, x, grid[y][x] - 1});
  while ((int)que.size() > 0) {
    pnt now = que.front();
    que.pop();
    if (grid[now.y][now.x] > now.cost) {
      grid[now.y][now.x] = now.cost;
    } else {
      continue;
    }
    for (auto &m : mv) {
      int ny, nx;
      ny = now.y + m[0];
      nx = now.x + m[1];
      if (ny < 0 || nx < 0 || ny > N - 1 || nx > N - 1) {
        continue;
      } else {
        que.push({ny, nx, now.cost + attend[ny][nx]});
      }
    }
  }
}
template <typename T>
void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}
template <typename T>
void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}
int main() {
  cin >> N;
  vector<vector<int>> grid(N, vector<int>(N, 0));
  vector<vector<int>> attend(N, vector<int>(N, 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      grid[i][j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    int y, x;
    y = p / N;
    x = p % N;
    ans += grid[y][x];
    bfs(y, x, grid, attend);
  }
  cout << ans << endl;
}
