#include <bits/stdc++.h>
using namespace std;
struct Node {
  bool done;
  int cost;
};
const int S[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
Node m[502][502];
int N;
int P[502][502] = {0};
int Q[502 * 502] = {0};
int solve(int num, int sx, int sy) {
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      m[i][j].cost = 100000000;
      m[i][j].done = false;
    }
  priority_queue<tuple<int, int, int>> que;
  m[sx][sy].cost = 0;
  m[sx][sy].done = true;
  que.push(make_tuple(-0, sx, sy));
  while (!que.empty()) {
    auto next = que.top();
    que.pop();
    int c(-get<0>(next)), cx(get<1>(next)), cy(get<2>(next));
    for (int d = 0; d < 4; d++) {
      int x = cx + S[d][0];
      int y = cy + S[d][1];
      if (P[x][y] == 0 || m[x][y].done) continue;
      int cost = c + (num < P[x][y] ? 1 : 0);
      m[x][y].cost = cost;
      m[x][y].done = true;
      auto t = make_tuple(-cost, x, y);
      que.push(t);
    }
  }
  int mcost = 100000000;
  for (int i = 1; i <= N; i++) {
    mcost = min(mcost, m[1][i].cost);
    mcost = min(mcost, m[N][i].cost);
    mcost = min(mcost, m[i][1].cost);
    mcost = min(mcost, m[i][N].cost);
  }
  return mcost;
}
int main() {
  cin >> N;
  for (int i = 1; i <= N * N; i++) {
    int pos;
    cin >> pos;
    P[(pos - 1) / N + 1][(pos - 1) % N + 1] = i;
    Q[i] = pos;
  }
  int sum = 0;
  for (int i = 1; i <= N * N; i++) {
    int pos = Q[i];
    int cost = solve(i, (pos - 1) / N + 1, (pos - 1) % N + 1);
    sum += cost;
  }
  cout << sum << endl;
}
