#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto i = begin(v); i != end(v); i++) is >> *i;
  return is;
}
template <class T>
T read_val() {
  throw 1;
}
template <>
int read_val() {
  int tmp;
  scanf("%" PRId32, &tmp);
  return tmp;
}
template <>
long long read_val() {
  long long tmp;
  scanf("%" PRId64, &tmp);
  return tmp;
}
auto read_int = read_val<int>;
auto read_long = read_val<long long>;
template <class T>
void scanf_vec(vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) vec[i] = read_val<T>();
}
vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n = read_int();
  vector<int> vec(n * n);
  scanf_vec(vec);
  vector<vector<bool>> board(n, vector<bool>(n, true));
  vector<int> visited(n * n, -1);
  vector<vector<int>> dists(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dists[i][j] = 1 + min({i, j, n - 1 - i, n - 1 - j});
    }
  }
  long long ans = 0;
  for (int v : vec) {
    v--;
    queue<int> pos;
    visited[v] = v;
    pos.push(v);
    ans += dists[v / n][v % n] - 1;
    board[v / n][v % n] = false;
    while (pos.size() > 0) {
      int x = pos.front();
      pos.pop();
      int row = x / n;
      int col = x % n;
      int min_other = n;
      for (int i = 0; i < 4; i++) {
        int r = row + dx[i], c = col + dy[i];
        if (r < 0 || c < 0 || r >= n || c >= n)
          min_other = 0;
        else {
          min_other = min(min_other, dists[r][c]);
        }
      }
      if (board[row][col]) min_other++;
      if (dists[row][col] <= min_other) continue;
      dists[row][col] = min_other;
      for (int i = 0; i < 4; i++) {
        int r = row + dx[i], c = col + dy[i];
        if (r < 0 || c < 0 || r >= n || c >= n)
          min_other = 0;
        else {
          int vv = r * n + c;
          if (visited[vv] != v) {
            visited[vv] = v;
            pos.push(vv);
          }
        }
      }
    }
  }
  printf("%" PRId64, ans);
  return 0;
}
