#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 62) - 1;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
inline char chin() {
  char x;
  scanf("%c", &x);
  return x;
}
inline string stin() {
  string x;
  cin >> x;
  return x;
}
inline double din() {
  double x;
  scanf("%lf", &x);
  return x;
}
template <typename T>
inline ll suma(const vector<T>& a) {
  ll res(0);
  for (auto&& x : a) res += x;
  return res;
}
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;
}
char itoa(int n) { return n + '0'; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  vvi c(n + 2, vi(n + 2, -1));
  for (int i = 1; i < n + 1; ++i)
    for (int j = 1; j < n + 1; ++j) c[i][j] = n * (i - 1) + j - 1;
  ll ans = 0;
  for (int _ = (0); _ < (n * n); ++_) {
    int p;
    cin >> p;
    p--;
    int sy = p / n + 1;
    int sx = p % n + 1;
    auto dijkstra = [&](int sy, int sx) {
      vector<vector<int>> d(n + 2, vector<int>(n + 2, INF));
      priority_queue<T, vector<T>, greater<T>> que;
      d[sy][sx] = 0;
      que.push(T(0, sy, sx));
      while (!que.empty()) {
        T p = que.top();
        que.pop();
        int cost, y, x;
        tie(cost, y, x) = p;
        if (d[y][x] < cost) continue;
        for (int i = (0); i < (4); ++i) {
          int ny = y + dy[i];
          int nx = x + dx[i];
          if (ny < 0 || y > n + 2 || x < 0 || nx > n + 2) continue;
          if (d[ny][nx] >
              d[y][x] + (c[ny][nx] == -2 || c[ny][nx] == -1 ? 0 : 1)) {
            d[ny][nx] = d[y][x] + (c[ny][nx] == -2 || c[ny][nx] == -1 ? 0 : 1);
            que.push(T(d[ny][nx], y, x));
          }
        }
      }
      return d;
    };
    auto d = dijkstra(sy, sx);
    int res = INF;
    for (int j = 1; j < n + 1; ++j) chmin(res, d[0][j]);
    for (int j = 1; j < n + 1; ++j) chmin(res, d[n + 1][j]);
    for (int i = 1; i < n + 1; ++i) chmin(res, d[i][0]);
    for (int i = 1; i < n + 1; ++i) chmin(res, d[i][n + 1]);
    ans += res;
    c[sy][sx] = -2;
  }
  cout << ans << endl;
  return 0;
}
