#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const int INF = 1 << 30;
struct Corr {
  int y;
  int x;
  int depth;
};
vector<int> x_vec = {0, 0, 1, -1};
vector<int> y_vec = {1, -1, 0, 0};
queue<Corr> q;
int height, width;
int x, y, depth;
vector<string> field;
int bfs(vector<vector<int>> cost) {
  int min_cost = INF;
  while (!q.empty()) {
    y = q.front().y, x = q.front().x, depth = q.front().depth;
    min_cost = min(min_cost, cost[y][x]);
    q.pop();
    if (field[y][x] == 'G') return 0;
    for (int i = 0; i < 4; ++i) {
      Corr next = {y + y_vec[i], x + x_vec[i], depth + 1};
      if (0 <= next.y && next.y < height && 0 <= next.x && next.x < width &&
          field[next.y][next.x] == '.') {
        field[next.y][next.x] = 'x';
        q.push(next);
      }
    }
  }
  return min_cost;
}
int main() {
  int N;
  cin >> N;
  vector<int> p(N * N);
  for (int i = 0; i < N * N; i++) cin >> p[i];
  field.resize(N + 2);
  height = N + 2, width = N + 2;
  for (int i = 0; i < height; i++) {
    field[i].resize(width);
    for (int j = 0; j < width; j++) field[i][j] = '#';
  }
  for (int i = 0; i < height; i++) {
    field[0][i] = 'G', field[height - 1][i] = 'G', field[i][0] = 'G',
    field[i][width - 1] = 'G';
  }
  vector<vector<int>> cost(N + 2, vector<int>(N + 2, 0));
  for (int i = 2; i <= N - 1; ++i) {
    for (int j = 2; j <= N - 1; ++j) {
      cost[i][j] = min({i - 1, j - 1, N - i, N - j});
    }
  }
  long long ans = 0;
  for (int i = 0; i < N * N; i++) {
    Corr start = {(p[i] - 1) / N + 1, (p[i] - 1) % N + 1, 0};
    queue<Corr>().swap(q);
    q.push(start);
    field[start.y][start.x] = '.';
    for (int j = 0; j < height; j++)
      for (int k = 0; k < width; k++)
        if (field[j][k] == 'x') field[j][k] = '.';
    for (int k = 0; k < height; k++) {
      for (int j = 0; j < width; j++) cout << field[k][j];
      cout << endl;
    }
    long long test = bfs(cost);
    ans += test;
  }
  cout << ans << endl;
}
