#include <bits/stdc++.h>
using namespace std;
int n;
int dist[505][505];
int jest[505][505];
void fill() {
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (n); ++j)
      dist[i][j] = min(min(i, j), min(n - 1 - i, n - 1 - j));
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (n); ++j) jest[i][j] = 1;
}
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
bool inside(int a, int b) { return (a >= 0 && b >= 0 && a < n && b < n); }
int compute(int x, int y) {
  int ans = dist[x][y];
  for (int i = (0); i < (4); ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!inside(nx, ny)) return 0;
    ans = min(ans, dist[nx][ny] + jest[nx][ny]);
  }
  return ans;
}
void update(int x, int y) {
  vector<pair<int, int> > que;
  que.push_back({x, y});
  int pos = 0;
  while (pos < que.size()) {
    int x = que[pos].first;
    int y = que[pos].second;
    pos++;
    for (int i = (0); i < (4); ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (!inside(nx, ny)) continue;
      int ndist = compute(nx, ny);
      if (ndist != dist[nx][ny]) {
        dist[nx][ny] = ndist;
        que.push_back({nx, ny});
      }
    }
  }
}
int main() {
  scanf("%d", &(n));
  ;
  fill();
  int ans = 0;
  vector<int>(v);
  for (int i = (0); i < ((n * n)); ++i) {
    int(a);
    scanf("%d", &(a));
    ;
    (v).push_back(a);
  };
  for (int i = (0); i < (v.size()); ++i) {
    int a = v[i] - 1;
    ans += dist[a % n][a / n];
    jest[a % n][a / n] = 0;
    update(a % n, a / n);
  }
  printf("%d\n", ans);
}
