#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < (n * n); ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> s(n, vector<int>(n, 0));
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (n); ++j) {
      s[i][j] = min({i, j, n - i - 1, n - j - 1});
    }
  vector<vector<bool>> t(n, vector<bool>(n, false));
  int ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    ans += s[p[i] / n][p[i] % n];
    t[p[i] / n][p[i] % n] = true;
    queue<int> q;
    for (int j = 0; j < (4); ++j) {
      if (p[i] / n + dx[j] >= 0 && p[i] / n + dx[j] < n &&
          p[i] % n + dy[j] >= 0 && p[i] % n + dy[j] < n) {
        q.push((p[i] / n + dx[j]) * n + p[i] % n + dy[j]);
      }
    }
    while (!q.empty()) {
      int num = q.front();
      q.pop();
      int x = num / n, y = num % n;
      bool flag = false;
      for (int j = 0; j < (4); ++j) {
        if (x + dx[j] >= 0 && x + dx[j] < n && y + dy[j] >= 0 &&
            y + dy[j] < n) {
          if (chmin(s[x][y], s[x + dx[j]][y + dy[j]] +
                                 (t[x + dx[j]][y + dy[j]] ? 0 : 1)))
            flag = true;
        }
      }
      if (flag) {
        for (int j = 0; j < (4); ++j) {
          if (x + dx[j] >= 0 && x + dx[j] < n && y + dy[j] >= 0 &&
              y + dy[j] < n) {
            q.push((x + dx[j]) * n + (y + dy[j]));
          }
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
