#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));
  std::vector<std::vector<bool> > zero_cost(N, std::vector<bool>(N, false));
  for (int i = 0; i < N; i++) {
    zero_cost[0][i] = true;
    zero_cost[N - 1][i] = true;
    zero_cost[i][0] = true;
    zero_cost[i][N - 1] = 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::deque<std::string> route;
    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);
    route.push_back("");
    while (!pos_x.empty()) {
      int x = pos_x[0];
      int y = pos_y[0];
      int cost_cur = cost[0];
      std::string route_cur = route[0];
      pos_x.pop_front();
      pos_y.pop_front();
      cost.pop_front();
      route.pop_front();
      if (zero_cost[y][x]) {
        min = cost_cur;
        min_all[y][x] = cost_cur;
        int str_index = route_cur.size() - 1;
        while (min_all[y][x] == cost_cur && str_index >= 0) {
          zero_cost[y][x] = true;
          if (route_cur[str_index] == '0') {
            x--;
          } else if (route_cur[str_index] == '1') {
            y--;
          } else if (route_cur[str_index] == '2') {
            x++;
          } else {
            y++;
          }
          str_index--;
        }
        break;
      }
      if (min_all[y][x] <= cost_cur) {
        continue;
      } else {
        min_all[y][x] = cost_cur;
      }
      if (min <= cost_cur) {
        continue;
      }
      if (exist[y][x + 1]) {
        pos_x.push_back(x + 1);
        pos_y.push_back(y);
        cost.push_back(cost_cur + 1);
        route.push_back(route_cur + "0");
      } else {
        pos_x.push_front(x + 1);
        pos_y.push_front(y);
        cost.push_front(cost_cur);
        route.push_back(route_cur + "0");
      }
      if (exist[y][x - 1]) {
        pos_x.push_back(x - 1);
        pos_y.push_back(y);
        cost.push_back(cost_cur + 1);
        route.push_back(route_cur + "2");
      } else {
        pos_x.push_front(x - 1);
        pos_y.push_front(y);
        cost.push_front(cost_cur);
        route.push_back(route_cur + "2");
      }
      if (exist[y + 1][x]) {
        pos_x.push_back(x);
        pos_y.push_back(y + 1);
        cost.push_back(cost_cur + 1);
        route.push_back(route_cur + "1");
      } else {
        pos_x.push_front(x);
        pos_y.push_front(y + 1);
        cost.push_front(cost_cur);
        route.push_back(route_cur + "1");
      }
      if (exist[y - 1][x]) {
        pos_x.push_back(x);
        pos_y.push_back(y - 1);
        cost.push_back(cost_cur + 1);
        route.push_back(route_cur + "3");
      } else {
        pos_x.push_front(x);
        pos_y.push_front(y - 1);
        cost.push_front(cost_cur);
        route.push_back(route_cur + "3");
      }
    }
    exist[cur_pos_y][cur_pos_x] = false;
    cnt += min;
  }
  std::cout << cnt << std::endl;
  return 0;
}
