#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int n;
bool check(int p) {
  if (p < 0 || p >= n)
    return false;
  else
    return true;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  V<int> p(n * n, -1);
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
  }
  V<V<int> > dist(n, V<int>(n));
  V<V<bool> > used(n, V<bool>(n, false));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) dist[i][j] = min({i, j, n - i - 1, n - j - 1});
  }
  V<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int x = p[i] / n, y = p[i] % n;
    ans += dist[x][y];
    used[x][y] = true;
    queue<P<int, int> > que;
    que.push(make_pair(x, y));
    while (!que.empty()) {
      P<int, int> q = que.front();
      que.pop();
      int tx = q.first, ty = q.second;
      for (int di = 0; di < 4; di++) {
        int nx = tx + dx[di], ny = ty + dy[di];
        if (!check(nx) || !check(ny)) continue;
        if (used[tx][ty] && dist[nx][ny] > dist[tx][ty]) {
          dist[nx][ny] = dist[tx][ty];
          que.push(make_pair(nx, ny));
        } else if (!used[nx][ny] && dist[nx][ny] > dist[tx][ty] + 1) {
          dist[nx][ny] = dist[tx][ty] + 1;
          que.push(make_pair(nx, ny));
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
