#include <bits/stdc++.h>
using namespace std;
template <typename T>
void setmax(T& x, T y) {
  x = max(x, y);
}
template <typename T>
void setmin(T& x, T y) {
  x = min(x, y);
}
const double PI = acos(-1.0);
const long long INF = 1e9 + 47;
const long long LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
namespace IntModulo {
const int mod = 1e9 + 7;
inline int add(int x, int y, int m = mod) {
  if (x + y < m) return x + y;
  return x + y - m;
}
inline int sub(int x, int y, int m = mod) {
  if (x >= y) return x - y;
  return x - y + m;
}
inline int mult(int x, int y, int m = mod) { return x * (long long)y % m; }
inline int power(int x, int y, int m = mod) {
  int r = 1;
  while (y) {
    if (y & 1) r = mult(r, x, m);
    x = mult(x, x, m);
    y >>= 1;
  }
  return r;
}
inline int inverse(int x, int m = mod) { return power(x, m - 2, m); }
inline void ADD(int& x, int y, int m = mod) {
  x += y;
  if (x >= m) x -= m;
}
inline void SUB(int& x, int y, int m = mod) {
  x -= y;
  if (x < 0) x += m;
}
inline void MULT(int& x, int y, int m = mod) { x = (x * (long long)y) % m; }
};  // namespace IntModulo
using namespace IntModulo;
const int N = 1 << 10;
int n;
int d[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int p[N * N];
int sz[N * N];
int ans[N * N];
int find(int x) {
  if (x == p[x]) {
    return x;
  }
  return p[x] = find(p[x]);
}
bool alive[N * N];
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (sz[x] < sz[y]) {
    swap(x, y);
  }
  p[y] = x;
  sz[x] += sz[y];
  setmin(ans[x], ans[y]);
}
int idx[N][N];
pair<int, int> IDX[N * N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  iota(p, p + N * N, 0);
  int ptr = 1;
  for (int i = (1); i < (n + 1); ++i)
    for (int j = (1); j < (n + 1); ++j) {
      idx[i][j] = ptr;
      IDX[ptr] = {i, j};
      ptr++;
    }
  for (int i = (0); i < (N * N); ++i) {
    alive[i] = true;
    sz[i] = 1;
  }
  for (int i = (1); i < (n + 1); ++i)
    for (int j = (1); j < (n + 1); ++j) {
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
      ans[idx[i][j]] = d[i][j];
      alive[idx[i][j]] = false;
    }
  long long res = 0;
  for (int it = (0); it < (n * n); ++it) {
    int x;
    cin >> x;
    int posi = IDX[x].first;
    int posj = IDX[x].second;
    alive[x] = true;
    for (int k = (0); k < (4); ++k) {
      int i = posi + dx[k];
      int j = posj + dy[k];
      if (alive[idx[i][j]]) {
        unite(idx[i][j], x);
      }
    }
    res += ans[find(x)];
  }
  cout << res << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}
