#include <bits/stdc++.h>
using namespace std;
void print_hate();
bool find_min_hate(int y, int x);
void updt_hate(int y, int x);
int* hate_buf;
bool* occp_buf;
int** hate;
bool** occp;
int N = 0;
int main() {
  int i = 0, j = 0;
  int next = 0;
  int hate_pairs = 0;
  cin >> N;
  hate_buf = new int[N * N];
  hate = new int*[N];
  for (i = 0; i < N; ++i) {
    hate[i] = hate_buf + i * N;
  }
  occp_buf = new bool[N * N];
  occp = new bool*[N];
  for (i = 0; i < N; ++i) {
    occp[i] = occp_buf + i * N;
  }
  for (i = 0; i < (N / 2 + N % 2); i++) {
    for (j = i; j < N - i; j++) {
      hate[i][j] = i;
      hate[j][i] = i;
      hate[N - i - 1][j] = i;
      hate[j][N - i - 1] = i;
    }
  }
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      occp[i][j] = true;
    }
  }
  for (i = 0; i < N * N; i++) {
    cin >> next;
    next--;
    occp[next / N][next % N] = false;
    hate_pairs += hate[next / N][next % N];
    updt_hate(next % N, next / N);
  }
  cout << hate_pairs;
}
void updt_hate(int y, int x) {
  find_min_hate(y, x);
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < i; j++) {
      (find_min_hate(y + (i - j), x + j));
      (find_min_hate(y + (i - j), x - j));
      (find_min_hate(y - (i - j), x + j));
      (find_min_hate(y - (i - j), x - j));
    }
  }
}
bool find_min_hate(int y, int x) {
  int min_hate = 0;
  int left = 0, right = 0, up = 0, down = 0;
  if (y < 0 || y >= N || x < 0 || x >= N) return false;
  if (y > 0) left = hate[y - 1][x] + occp[y - 1][x];
  if (y < N - 1) right = hate[y + 1][x] + occp[y + 1][x];
  if (x > 0) up = hate[y][x - 1] + occp[y][x - 1];
  if (x < N - 1) down = hate[y][x + 1] + occp[y][x + 1];
  min_hate = min(min(min(left, right), up), down);
  hate[y][x] = min_hate;
  return true;
}
void print_hate() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << hate[i][j] << " ";
    }
    cout << "   ";
    for (int j = 0; j < N; j++) {
      cout << occp[i][j] << " ";
    }
    cout << "\n";
  }
  cout << "\n";
}
