#include <bits/stdc++.h>
using lint = long long;
template <typename T>
void print_container(std::vector<T> vec) {
  for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
}
class Seat {
 public:
  lint y;
  lint x;
  lint dir;
  Seat(lint y, lint x, lint dir) : y(y), x(x), dir(dir) {}
};
int main() {
  lint n;
  scanf("%lld", &n);
  std::vector<lint> p(n * n);
  std::vector<std::vector<lint>> seats(n, std::vector<lint>(n));
  for (int i = 0; i < n * n; ++i) {
    scanf("%lld", &p[i]);
  }
  for (lint i = 0; i < n; ++i) {
    for (lint j = 0; j < n; ++j) {
      lint close_y = std::min(i, n - i - 1);
      lint close_x = std::min(j, n - j - 1);
      seats[i][j] = std::min(close_y, close_x);
    }
  }
  lint ans = 0;
  std::deque<Seat> q;
  std::vector<std::pair<lint, lint>> dirs(4);
  dirs[0] = std::make_pair(1, 0);
  dirs[1] = std::make_pair(0, 1);
  dirs[2] = std::make_pair(0, -1);
  dirs[3] = std::make_pair(-1, 0);
  for (lint i = 0; i < n * n; ++i) {
    lint seat_y = (p[i] - 1) / n;
    lint seat_x = (p[i] - 1) % n;
    ans += seats[seat_y][seat_x];
    lint min_cost = 10000000;
    if (seats[seat_y][seat_x] == 0) {
      seats[seat_y][seat_x] = -1;
    } else {
      for (auto dir : dirs) {
        lint y = seat_y + dir.first;
        lint x = seat_x + dir.second;
        if (y >= n || y < 0 || x >= n || x < 0) {
          continue;
        }
        min_cost = std::min(min_cost, seats[y][x]);
      }
      seats[seat_y][seat_x] = min_cost;
    }
    q.push_back(Seat(seat_y, seat_x, 5));
    while (!q.empty()) {
      auto cur = q.front();
      q.pop_front();
      for (int i = 0; i < 4; ++i) {
        lint y = cur.y + dirs[i].first;
        lint x = cur.x + dirs[i].second;
        if (y >= n || y < 0 || x >= n || x < 0 || cur.dir + i == 3) {
          continue;
        }
        if (seats[y][x] > seats[cur.y][cur.x] + 1) {
          seats[y][x] = seats[cur.y][cur.x] + 1;
          q.push_back(Seat(y, x, i));
        }
      }
    }
  }
  std::cout << ans << "\n";
  return 0;
}
