#include <bits/stdc++.h>
std::queue<std::pair<std::pair<int, int>, int> > Q;
int x[250010];
int check[510][510], weight[510][510], check2[510][510];
std::pair<int, int> next[510][510];
int direX[5] = {1, 0, -1, 0}, direY[5] = {0, 1, 0, -1};
std::pair<int, int> find(int s, int t) {
  if (next[s][t] == std::make_pair(s, t))
    return std::make_pair(s, t);
  else
    return next[s][t] = find(next[s][t].first, next[s][t].second);
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 1; i <= a * a; i++) scanf("%d", &x[i]);
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      int s1 = (i - 1) < (a - i) ? (i - 1) : (a - i);
      int s2 = (j - 1) < (a - j) ? (j - 1) : (a - j);
      check[i][j] = s1 < s2 ? s1 : s2;
    }
  }
  for (int i = 0; i <= a + 1; i++)
    for (int j = 0; j <= a + 1; j++) next[i][j] = std::make_pair(i, j);
  for (int i = 0; i <= a + 1; i++)
    for (int j = 0; j <= a + 1; j++) weight[i][j] = 0;
  long long int sum = 0;
  for (int i = 1; i <= a * a; i++) {
    int s = (x[i] - 1) / a + 1;
    int t = (x[i] - 1) % a + 1;
    next[s][t] = std::make_pair(s, t);
    weight[s][t] = check[s][t];
    check2[s][t] = 1;
    for (int j = 0; j < 4; j++) {
      int s2 = s + direX[j], t2 = t + direY[j];
      if (check2[s2][t2] == 1) {
        std::pair<int, int> P = find(s2, t2);
        std::pair<int, int> P2 = find(s, t);
        if (P == P2) continue;
        next[P2.first][P2.second] = P;
        weight[P.first][P.second] =
            weight[P.first][P.second] < weight[P2.first][P2.second]
                ? weight[P.first][P.second]
                : weight[P2.first][P2.second];
      }
    }
    std::pair<int, int> P = find(s, t);
    sum += weight[P.first][P.second];
  }
  printf("%lld", sum);
}
