#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 99824353;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 99824353;
    a = 1LL * a * a % 99824353;
    b >>= 1;
  }
  return ans;
}
int pow(int a, long long b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
int p[501 * 501], l[501][501], u[501][501], r[501][501], d[501][501],
    dp[501][501];
priority_queue<pair<int, pair<int, int>>> q;
int n;
void Push(int first, int second, int w) {
  if (first < 0 || first >= n || second < 0 || second >= n) return;
  if (dp[first][second] > w) {
    dp[first][second] = w;
    q.push({-w, {first, second}});
  }
}
int main() {
  int ca = 0, T, i, j, k, m, K;
  scanf("%d", &n);
  m = n * n;
  for (int i = 0; i < m; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
      r[i][j] = j + 1;
      l[i][j] = j - 1;
      u[i][j] = i - 1;
      d[i][j] = i + 1;
    }
  }
  int ans = 0, w;
  for (int i = 0; i < m; i++) {
    int first = p[i] / n, second = p[i] % n;
    w = dp[first][second];
    ans += dp[first][second];
    Push(first, l[first][second], w);
    Push(first, r[first][second], w);
    Push(u[first][second], second, w);
    Push(d[first][second], second, w);
    while (!q.empty()) {
      w = -q.top().first;
      first = q.top().second.first, second = q.top().second.second;
      q.pop();
      Push(first, l[first][second], w + 1);
      Push(first, r[first][second], w + 1);
      Push(u[first][second], second, w + 1);
      Push(d[first][second], second, w + 1);
    }
    int y1 = l[first][second], y2 = r[first][second];
    if (y1 >= 0) r[first][y1] = y2;
    l[first][y2] = y1;
    int x1 = u[first][second], x2 = d[first][second];
    if (x1 >= 0) d[x1][second] = x2;
    u[x2][second] = x1;
  }
  printf("%d\n", ans);
}
