#include <bits/stdc++.h>
int dist[505][505];
int last[505][505];
int tick;
int n;
int alive[505][505];
std::vector<std::pair<int, int> > dirs{
    std::make_pair(0, 1), std::make_pair(1, 0), std::make_pair(-1, 0),
    std::make_pair(0, -1)};
bool bounds(std::pair<int, int> p) {
  return p.first >= 0 && p.first < n && p.second >= 0 && p.second < n;
}
std::pair<int, int> operator+(std::pair<int, int> a, std::pair<int, int> b) {
  return std::make_pair(a.first + b.first, a.second + b.second);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (n); ++j)
      dist[i][j] = std::min(i, std::min(j, std::min(n - 1 - i, n - 1 - j)));
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (n); ++j) alive[i][j] = 1;
  long long ans = 0;
  tick = 5;
  for (int i = 0; i < (n * n); ++i) {
    int a;
    std::cin >> a;
    a--;
    int x = a / n;
    int y = a % n;
    alive[x][y] = 0;
    ans += dist[x][y];
    auto start = std::make_pair(x, y);
    std::queue<std::pair<int, int> > que;
    for (auto dir : dirs) {
      auto pos = dir + start;
      if (!bounds(pos)) continue;
      que.push(pos);
      last[pos.first][pos.second] = tick;
    }
    while (((int)(que).size())) {
      auto v = que.front();
      que.pop();
      int old = dist[v.first][v.second];
      int was = 0;
      for (auto dir : dirs) {
        auto pos = dir + v;
        if (!bounds(pos)) continue;
        if (dist[pos.first][pos.second] + alive[pos.first][pos.second] < old)
          was = 1;
      }
      if (was) {
        dist[v.first][v.second]--;
        for (auto dir : dirs) {
          auto pos = dir + v;
          if (!bounds(pos)) continue;
          if (last[pos.first][pos.second] != tick) {
            last[pos.first][pos.second] = tick;
            que.push(pos);
          }
        }
      }
    }
    tick++;
  }
  std::cout << ans;
  return 0;
}
