#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
class disjoint_set {
 private:
  std::vector<size_t> parents;
  std::vector<size_t> ranks;

 public:
  const size_t n_vertices;
  disjoint_set(size_t n_vertices)
      : n_vertices(n_vertices), parents(n_vertices), ranks(n_vertices, 0) {
    for (size_t i = 0; i < n_vertices; ++i) {
      parents[i] = i;
    }
  }
  size_t root(size_t x) {
    if (parents[x] == x) {
      return x;
    } else {
      return parents[x] = root(parents[x]);
    }
  }
  void merge(size_t x, size_t y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (ranks[x] < ranks[y]) {
      parents[x] = y;
    } else {
      parents[y] = x;
      if (ranks[x] == ranks[y]) {
        ++ranks[x];
      }
    }
  }
  bool is_same(size_t x, size_t y) { return root(x) == root(y); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int i = 0; i < (P.size()); ++i) {
    cin >> P[i];
    --P[i];
  }
  vector<int> dist(N * N);
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (N); ++j) {
      dist[i * N + j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  disjoint_set ds(N * N);
  set<int> vacant;
  int result = 0;
  for (int p : P) {
    int i = p / N;
    int j = p % N;
    for (int k = 0; k < (4); ++k) {
      int in = i + dx[k];
      int jn = j + dy[k];
      int pn = in * N + jn;
      if (0 <= in && in < N && 0 <= jn && jn < N && vacant.count(pn)) {
        int dist_next = min(dist[ds.root(p)], dist[ds.root(pn)]);
        ds.merge(p, pn);
        dist[ds.root(p)] = dist_next;
      }
    }
    result += dist[ds.root(p)];
    vacant.insert(p);
  }
  cout << result << endl;
  return 0;
}
