#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;
const int maxN = 505;
int n;
int mat[maxN][maxN];
int get_row(int x) { return x / n; }
int get_col(int x) { return x % n; }
long long get_cost(int r, int c) {
  map<pair<int, int>, long long> d;
  d[{r, c}] = 0;
  deque<pair<int, int> > q;
  q.push_front({r, c});
  while (!q.empty()) {
    pair<int, int> v = q.front();
    q.pop_front();
    int row = v.first;
    int col = v.second;
    if (row == 0 || row == n - 1 || col == 0 || col == n - 1)
      return d[{row, col}];
    if (d.find({row + 1, col}) == d.end() ||
        d[{row, col}] + mat[row + 1][col] < d[{row + 1, col}]) {
      d[{row + 1, col}] = d[{row, col}] + mat[row + 1][col];
      if (mat[row + 1][col] == 1)
        q.push_back({row + 1, col});
      else
        q.push_front({row + 1, col});
    }
    if (d.find({row - 1, col}) == d.end() ||
        d[{row, col}] + mat[row - 1][col] < d[{row - 1, col}]) {
      d[{row - 1, col}] = d[{row, col}] + mat[row - 1][col];
      if (mat[row - 1][col] == 1)
        q.push_back({row - 1, col});
      else
        q.push_front({row - 1, col});
    }
    if (d.find({row, col + 1}) == d.end() ||
        d[{row, col}] + mat[row][col + 1] < d[{row, col + 1}]) {
      d[{row, col + 1}] = d[{row, col}] + mat[row][col + 1];
      if (mat[row][col + 1] == 1)
        q.push_back({row, col + 1});
      else
        q.push_front({row, col + 1});
    }
    if (d.find({row, col - 1}) == d.end() ||
        d[{row, col}] + mat[row][col - 1] < d[{row, col - 1}]) {
      d[{row, col - 1}] = d[{row, col}] + mat[row][col - 1];
      if (mat[row][col - 1] == 1)
        q.push_back({row, col - 1});
      else
        q.push_front({row, col - 1});
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mat[i][j] = 1;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    int aux;
    scanf("%d", &aux);
    aux--;
    int row = get_row(aux);
    int col = get_col(aux);
    mat[row][col] = 0;
    ans += get_cost(row, col);
  }
  printf("%lld\n", ans);
  return 0;
}
