#include <bits/stdc++.h>
using namespace std;
int m[555][555];
int d[555][555];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  int num;
  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    for (int j = 0; j < num; j++) {
      d[i + 1][j + 1] = min(min(i, j), min(num - 1 - i, num - 1 - j));
      m[i + 1][j + 1] = 1;
    }
  }
  int ans = 0;
  for (int p = 0; p < num * num; p++) {
    int z;
    scanf("%d", &z);
    z--;
    int x = z / num, y = z % num;
    x++, y++;
    deque<pair<int, int> > que;
    que.push_back(make_pair(x, y));
    for (;;) {
      if (que.empty()) break;
      pair<int, int> z = que.front();
      que.pop_front();
      int mini = 1000000;
      for (int i = 0; i < 4; i++)
        mini = min(mini, d[z.first + dx[i]][z.second + dy[i]]);
      if (m[z.first][z.second]) mini++;
      if (mini >= d[z.first][z.second]) continue;
      d[z.first][z.second] = mini;
      for (int i = 0; i < 4; i++) {
        if (m[z.first + dx[i]][z.second + dy[i]])
          que.push_back(make_pair(z.first + dx[i], z.second + dy[i]));
        else
          que.push_front(make_pair(z.first + dx[i], z.second + dy[i]));
      }
    }
    ans += d[x][y];
  }
  printf("%d\n", ans);
}
