#include <bits/stdc++.h>
using lnt = long long;
constexpr lnt INF = 2e18;
int main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int> > cost(n, std::vector<int>(n));
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (n); ++j) {
      cost[i][j] = std::min({i, j, n - i - 1, n - j - 1});
    }
  };
  std::vector<int> p(n * n);
  for (int i = 0; i < (n * n); ++i) std::cin >> p[i];
  for (int i = 0; i < (n * n); ++i) p[i]--;
  std::vector<std::vector<int> > alive(n, std::vector<int>(n, true));
  lnt sum = 0;
  for (auto q : p) {
    int x = q / n, y = q % n;
    sum += cost[x][y];
    ;
    std::priority_queue<std::pair<int, std::pair<int, int> > > que;
    alive[x][y] = false;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dy != 0 || (dx == 0 && dy == 0)) continue;
        if (x + dx < 0 || x + dx >= n) {
          cost[x][y] = -1;
          continue;
        }
        if (y + dy < 0 || y + dy >= n) {
          cost[x][y] = -1;
          continue;
        }
        cost[x][y] = std::min(cost[x][y], cost[x + dx][y + dy]);
      }
    };
    que.emplace(n - cost[x][y], std::make_pair(x, y));
    while (!que.empty()) {
      auto xx = que.top();
      que.pop();
      int i = xx.second.first, j = xx.second.second;
      for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
          if (i + dx < 0 || i + dx >= n) continue;
          if (j + dy < 0 || j + dy >= n) continue;
          if (dx * dy != 0 || (dx == 0 && dy == 0)) continue;
          if (alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j] + 1) {
            cost[i + dx][j + dy] = cost[i][j] + 1;
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
          if (!alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j]) {
            cost[i + dx][j + dy] = cost[i][j];
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
        }
      }
    };
    ;
    ;
  }
  std::cout << (sum) << '\n';
}
