#include <bits/stdc++.h>
using namespace std;
int dist[500][500];
bool used[500][500];
vector<pair<int, int>> dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int n;
bool check(int first, int second) {
  return first >= 0 && first < n && second < n && second >= 0;
}
void dfs(int first, int second, int d) {
  dist[first][second]--;
  for (auto el : dir) {
    int x2 = el.first + first;
    int y2 = el.second + second;
    if (check(x2, y2))
      if (used[first][second] && dist[x2][y2] != dist[first][second]) {
        dfs(x2, y2, d);
      } else if (dist[x2][y2] == d) {
        dfs(x2, y2, d + 1);
      }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<pair<int, int>> order;
  for (int i = 0; i < n * n; i++) {
    int num;
    cin >> num;
    num--;
    int first = num / n;
    int second = num % n;
    order.push_back({first, second});
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  long long ans = 0;
  for (auto el : order) {
    ans += dist[el.first][el.second];
    used[el.first][el.second] = 1;
    for (auto el2 : dir) {
      int first = el.first + el2.first;
      int second = el.second + el2.second;
      if (check(first, second) &&
          dist[first][second] == dist[el.first][el.second] + 1) {
        dfs(first, second, dist[first][second] + 1);
      }
    }
  }
  cout << ans;
  return 0;
}
