#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long int dd[600][600];
typedef struct UnionFind {
  vector<long long int> par;
  vector<long long int> size;
  UnionFind(int N) {
    for (int i = 0; i < N; i++) {
      par.push_back(i);
      size.push_back(1);
    }
  }
  long long int root(long long int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(long long int x, long long int y) {
    long long int rx = root(x);
    long long int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
    long long int resize = size[rx] + size[ry];
    size[rx] = resize;
    size[ry] = resize;
  }
  bool same(long long int x, long long int y) {
    long long int rx = root(x);
    long long int ry = root(y);
    return rx == ry;
  }
  long long int getSize(long long int x) {
    long long int rx = root(x);
    return size[rx];
  }
};
int main() {
  long long int n;
  cin >> n;
  for (int i = 0; i < 600; i++)
    for (int j = 0; j < 600; j++) {
      dd[i][j] = 1;
      if (i == 0 || i == n + 1 || j == 0 || j == n + 1) dd[i][j] = 0;
    }
  long long int ans = 0;
  for (int _ = 0; _ < n * n; _++) {
    long long int a;
    cin >> a;
    long long int nowY = (a - 1) / n + 1;
    long long int nowX = (a - 1) % n + 1;
    priority_queue<
        pair<long long int, pair<long long int, long long int>>,
        vector<pair<long long int, pair<long long int, long long int>>>,
        greater<pair<long long int, pair<long long int, long long int>>>>
        q;
    q.push({dd[nowY][nowX], {nowX, nowY}});
    set<pair<long long int, long long int>> s;
    long long int dx[4] = {-1, 0, 1, 0};
    long long int dy[4] = {0, 1, 0, -1};
    while (q.size()) {
      pair<long long int, long long int> now = q.top().second;
      long long int nowD = q.top().first;
      long long int nowX = now.first;
      long long int nowY = now.second;
      q.pop();
      s.insert({nowX, nowY});
      if (nowX <= -1 || nowX >= n + 2 || nowY <= -1 || nowY >= n + 2) continue;
      if (nowD > n) continue;
      if (nowX == 0 || nowX == n + 1 || nowY == 0 || nowY == n + 1) {
        if (0)
          cout << "a"
               << "=" << a << " "
               << "nowY"
               << "=" << nowY << " "
               << "nowX"
               << "=" << nowX << " "
               << "nowD"
               << "=" << nowD << endl;
        ;
        ans += (nowD - 1);
        break;
      }
      for (int k = 0; k < 4; k++) {
        long long int nx = nowX + dx[k];
        long long int ny = nowY + dy[k];
        if (s.find({nx, ny}) != s.end()) continue;
        q.push({nowD + dd[ny][nx], {nx, ny}});
      }
    }
    dd[nowY][nowX] = 0;
  }
  cout << ans << endl;
  return 0;
}
