#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const double EPS = 1e-8;
long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1) ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}
template <typename T, typename U>
bool chmax(T& max, const U& x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}
template <typename T, typename U>
bool chmin(T& min, const U& x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}
bool occupied[502][502];
long long temp[502][502];
bool passed[502][502];
long long n;
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, 1, 0, -1};
long long bfs(long long x, long long y) {
  fill(passed[0], passed[0] + 502 * 502, false);
  priority_queue<vector<long long>> que;
  vector<long long> v = {0, x, y};
  que.push(v);
  passed[x][y] = true;
  while (que.size()) {
    vector<long long> t = que.top();
    que.pop();
    if (t[1] == 0 || t[1] == n + 1 || t[2] == 0 || t[2] == n + 1) {
      return -t[0];
    }
    for (long long i = 0; i < 4; i++) {
      long long xx = t[1] + dx[i];
      long long yy = t[2] + dy[i];
      if (!passed[xx][yy]) {
        que.push({t[0] - occupied[xx][yy], xx, yy});
        passed[xx][yy] = true;
      }
    }
  }
  return 0;
}
int main() {
  cin >> n;
  long long a[n * n];
  long long ans = 0;
  fill(occupied[0], occupied[0] + 502 * 502, false);
  fill(temp[0], temp[0] + 502 * 502, n / 2 + 1);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      occupied[i][j] = true;
    }
  }
  for (long long i = 0; i < n * n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n * n; i++) {
    long long x = 0, y = 1;
    while (a[i] > n) {
      a[i] -= n;
      y++;
    }
    x = a[i];
    occupied[x][y] = false;
    ans = ans + bfs(x, y);
  }
  cout << ans << endl;
}
