#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long big = 1e18;
const double PI = 2 * asin(1);
int main() {
  int N;
  cin >> N;
  int P[N * N];
  for (int i = 0; i < N * N; ++i) {
    cin >> P[i];
    P[i]--;
  }
  bool seat[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      seat[i][j] = false;
    }
  }
  int ansarr[N][N];
  int ans = 0;
  int tmpans;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int> >,
                 greater<tuple<int, int, int> > >
      que;
  tuple<int, int, int> state;
  int x, y, cost;
  for (int i = 0; i < N * N; ++i) {
    for (int j = 0; j < N; ++j) {
      for (int k = 0; k < N; ++k) {
        ansarr[j][k] = -1;
      }
    }
    que.push(make_tuple(0, P[i] / N, P[i] % N));
    seat[P[i] / N][P[i] % N] = true;
    while (!que.empty()) {
      state = que.top();
      que.pop();
      x = get<1>(state);
      y = get<2>(state);
      cost = get<0>(state);
      if (ansarr[x][y] > -1) continue;
      ansarr[x][y] = cost;
      if (x == 0 || x == N - 1 || y == 0 || y == N - 1) {
        ans += cost;
        break;
      }
      if (x > 0 && ansarr[x - 1][y] == -1) {
        if (seat[x - 1][y] == false)
          que.push(make_tuple(cost + 1, x - 1, y));
        else
          que.push(make_tuple(cost, x - 1, y));
      }
      if (y > 0 && ansarr[x][y - 1] == -1) {
        if (seat[x][y - 1] == false)
          que.push(make_tuple(cost + 1, x, y - 1));
        else
          que.push(make_tuple(cost, x, y - 1));
      }
      if (x < N - 1 && ansarr[x + 1][y] == -1) {
        if (seat[x + 1][y] == false)
          que.push(make_tuple(cost + 1, x + 1, y));
        else
          que.push(make_tuple(cost, x + 1, y));
      }
      if (y < N - 1 && ansarr[x][y + 1] == -1) {
        if (seat[x][y + 1] == false)
          que.push(make_tuple(cost + 1, x, y + 1));
        else
          que.push(make_tuple(cost, x, y + 1));
      }
    }
    while (!que.empty()) que.pop();
  }
  cout << ans << endl;
}
