#include <bits/stdc++.h>
int main() {
  int N;
  std::cin >> N;
  int N2 = N * N;
  std::vector<int> P(N2);
  for (int i = 0; i < N2; i++) {
    std::cin >> P[i];
  }
  std::vector<std::vector<bool> > exist(N, std::vector<bool>(N, true));
  int cnt = 0;
  for (int i = 0; i < N2; i++) {
    std::deque<int> pos_x;
    std::deque<int> pos_y;
    std::deque<int> cost;
    std::vector<std::vector<int> > min_all(N, std::vector<int>(N, 1e9));
    int cur_pos_x = (P[i] - 1) % N;
    int cur_pos_y = (P[i] - 1) / N;
    int min = 1e9;
    pos_x.push_back(cur_pos_x);
    pos_y.push_back(cur_pos_y);
    cost.push_back(0);
    while (!pos_x.empty()) {
      int x = pos_x[0];
      int y = pos_y[0];
      int cost_cur = cost[0];
      pos_x.pop_front();
      pos_y.pop_front();
      cost.pop_front();
      if (x == 0 || x == N - 1 || y == 0 || y == N - 1) {
        if (min > cost_cur) {
          min = cost_cur;
        }
        continue;
      }
      if (min_all[y][x] <= cost_cur) {
        continue;
      } else {
        min_all[y][x] = cost_cur;
      }
      if (min <= cost_cur) {
        continue;
      }
      pos_x.push_back(x + 1);
      pos_y.push_back(y);
      cost.push_back(cost_cur + exist[y][x + 1]);
      pos_x.push_back(x - 1);
      pos_y.push_back(y);
      cost.push_back(cost_cur + exist[y][x - 1]);
      pos_x.push_back(x);
      pos_y.push_back(y + 1);
      cost.push_back(cost_cur + exist[y + 1][x]);
      pos_x.push_back(x);
      pos_y.push_back(y - 1);
      cost.push_back(cost_cur + exist[y - 1][x]);
    }
    exist[cur_pos_y][cur_pos_x] = false;
    cnt += min;
  }
  std::cout << cnt << std::endl;
  return 0;
}
