#include <bits/stdc++.h>
using namespace std;
int cost[500][500];
bool valid[500][500];
int N;
const int INF_COST = 500;
int get(int x, int y) {
  if (x >= N || x < 0 || y >= N || y < 0)
    return INF_COST;
  else
    return cost[x][y];
}
int cc(int min, int x, int y) {
  int c = valid[x][y] ? (cost[x][y] + 1) : (cost[x][y]);
  if (min > c) {
    return c;
  } else {
    return INF_COST;
  }
}
void update(int x, int y) {
  if (x >= N || x < 0 || y >= N || y < 0) return;
  int min = cost[x][y];
  bool updated = false;
  int cos = INF_COST;
  if ((cos = cc(min, x + 1, y)) != INF_COST) {
    min = cos;
    updated = true;
  } else if ((cos = cc(min, x, y + 1)) != INF_COST) {
    min = cos;
    updated = true;
  } else if ((cos = cc(min, x - 1, y)) != INF_COST) {
    min = cos;
    updated = true;
  } else if ((cos = cc(min, x, y - 1)) != INF_COST) {
    min = cos;
    updated = true;
  }
  cost[x][y] = min;
  if (updated) {
    update(x + 1, y);
    update(x, y + 1);
    update(x - 1, y);
    update(x, y - 1);
  }
}
int main() {
  scanf("%d", &N);
  for (int x = 0; x < N; ++x) {
    for (int y = 0; y < N; ++y) {
      cost[x][y] = min(min(x, N - 1 - x), min(y, N - 1 - y));
      valid[x][y] = true;
    }
  }
  int count = 0;
  for (int i = 0; i < N * N; ++i) {
    int id;
    scanf("%d", &id);
    int x = (id - 1) % N;
    int y = (id - 1) / N;
    count += cost[x][y];
    valid[x][y] = false;
    update(x + 1, y);
    update(x, y + 1);
    update(x - 1, y);
    update(x, y - 1);
  }
  printf("%d", count);
}
