#include <bits/stdc++.h>
long long N;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> N;
  std::vector<std::vector<long long>> v(N, std::vector<long long>(N));
  std::vector<std::vector<bool>> taken(N, std::vector<bool>(N, true));
  for (long long i = 0; i < N; i++)
    for (long long j = 0; j < N; j++)
      v[i][j] = std::min(std::min(i, N - i - 1), std::min(j, N - j - 1));
  long long res = 0;
  for (int i = 0; i < N * N; i++) {
    long long idx;
    std::cin >> idx;
    idx--;
    long long sr = idx / N, sc = idx % N;
    std::queue<std::pair<long long, std::pair<long long, long long>>> Q;
    Q.push({0, {sr, sc}});
    std::set<std::pair<long long, long long>> vis;
    while (!Q.empty()) {
      std::pair<long long, std::pair<long long, long long>> cur = Q.front();
      Q.pop();
      long long d = cur.first;
      long long r = cur.second.first, c = cur.second.second;
      if (vis.find(cur.second) != vis.end()) continue;
      vis.insert(cur.second);
      if (r == 0 || r == N - 1 || c == 0 || c == N - 1) {
        v[sr][sc] = std::min(v[sr][sc], d);
        continue;
      }
      if (d >= v[sr][sc]) continue;
      Q.push({d + taken[r - 1][c], {r - 1, c}});
      Q.push({d + taken[r + 1][c], {r + 1, c}});
      Q.push({d + taken[r][c - 1], {r, c - 1}});
      Q.push({d + taken[r][c + 1], {r, c + 1}});
    }
    res += v[sr][sc];
    taken[sr][sc] = false;
  }
  std::cout << res << "\n";
}
