#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 510;
int n, dis[maxn][maxn], od[maxn][maxn];
int p[maxn];
int get_ans(int x, int y) {
  static map<int, int> dp[maxn];
  int res = min({x - 1, n - x, y - 1, n - y});
  auto dis = [&](int a, int b, int c, int d) {
    return abs(a - c) + abs(b - d) - 1;
  };
  for (int i = 1; i <= n; ++i) {
    auto it = dp[i].lower_bound(y);
    if (it != end(dp[i])) res = min(res, it->second + dis(i, it->first, x, y));
    if (it != begin(dp[i]))
      --it, res = min(res, it->second + dis(i, it->first, x, y));
  }
  for (int i = 1; i <= n; ++i) {
    auto it = dp[i].lower_bound(y);
    if (it != end(dp[i]))
      it->second = min(it->second, res + dis(i, it->first, x, y));
    if (it != begin(dp[i]))
      --it, it->second = min(it->second, res + dis(i, it->first, x, y));
  }
  return dp[x][y] = res;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  int res = 0;
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    --p[i];
    int r = p[i] / n, c = p[i] % n;
    res += get_ans(r + 1, c + 1);
  }
  cout << res << '\n';
}
