#include <bits/stdc++.h>
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, x[505], y[505], G[505][505], d[505][505];
int ans = 0;
void bfs(int x, int y) {
  std::deque<std::pair<int, int> > q;
  q.push_back(std::make_pair(x, y));
  while (!q.empty()) {
    std::pair<int, int> u = q.front();
    q.pop_back();
    for (int dr = 0; dr < 4; ++dr) {
      int x1 = u.first + dx[dr];
      int y1 = u.second + dy[dr];
      if (x1 < 1 || x1 > n || y1 < 1 || y1 > n) continue;
      if (d[x1][y1] > d[u.first][u.second] + G[x1][y1]) {
        d[x1][y1] = d[u.first][u.second] + G[x1][y1];
        if (G[x1][y1])
          q.push_back(std::make_pair(x1, y1));
        else
          q.push_front(std::make_pair(x1, y1));
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; ++i) {
    int p;
    scanf("%d", &p);
    x[i] = (p - 1) / n + 1;
    y[i] = (p - 1) % n + 1;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      d[i][j] = std::min({i, j, n - i + 1, n - j + 1});
      G[i][j] = 1;
    }
  for (int i = 1; i <= n * n; i++) {
    d[x[i]][y[i]]--;
    G[x[i]][y[i]] = 0;
    ans += d[x[i]][y[i]];
    bfs(x[i], y[i]);
  }
  printf("%d", ans);
  return 0;
}
