#include <bits/stdc++.h>
using namespace std;
void Debug() { cerr << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cerr << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = int(0); i < int((int)(v).size() - 1); i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = (1 << 30) - 1;
const long long linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng;
int N;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int P[MAX_N];
int D[510][510];
int C[510][510];
void solve() {
  cin >> N;
  for (int i = int(0); i < int(N); i++) {
    for (int j = int(0); j < int(N); j++) {
      D[i][j] = min({i, j, N - 1 - i, N - 1 - j});
      C[i][j] = 1;
    }
  }
  queue<pair<int, int> > que;
  int res = 0;
  for (int q = int(0); q < int(N * N); q++) {
    int a;
    cin >> a;
    a--;
    int x = a / N, y = a % N;
    res += D[x][y];
    C[x][y] = 0;
    que.push(pair<int, int>(x, y));
    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();
      for (int i = int(0); i < int(4); i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (D[nx][ny] > D[x][y] + C[x][y]) {
          D[nx][ny] = D[x][y] + C[x][y];
          que.push(pair<int, int>(nx, ny));
        }
      }
    }
  }
  cout << res << "\n";
}
uint32_t rd() {
  uint32_t res;
  res = std::random_device()();
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
  rng.seed(rd());
  solve();
  cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
