#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500;
int grid[MAX_N][MAX_N];
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 main() {
  int N;
  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;
  bool checked[N][N];
  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;
    queue<Node> que;
    que.push(Node(y, x, v - 1));
    bool visited[N][N];
    memset(visited, false, sizeof(visited));
    while (!que.empty()) {
      Node node = que.front();
      que.pop();
      if (grid[node.y][node.x] <= node.dist) continue;
      if (visited[node.y][node.x]) continue;
      visited[node.y][node.x] = true;
      grid[node.y][node.x] = max(0, min(grid[node.y][node.x], node.dist));
      for (int i = 0; i < 4; ++i) {
        int ny = node.y + DY[i];
        int nx = node.x + DX[i];
        if (ny < 0 || nx < 0 || N <= ny || N <= nx) continue;
        if (checked[ny][nx]) {
          que.push(Node(ny, nx, node.dist));
        } else {
          que.push(Node(ny, nx, node.dist + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
