#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;
  auto bfs = [&](int sy, int sx, int gy, int gx) {
    vvi d(n + 2, vi(n + 2, INF));
    d[sy][sx] = 0;
    queue<P> q;
    q.push(P(sy, sx));
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      int y = p.first, x = p.second;
      if (y == gy && x == gx) break;
      for (int i = (0); i < (4); ++i) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (!(((0) <= (ny) && (ny) < (n + 2)) &&
              ((0) <= (nx) && (nx) < (n + 2))))
          continue;
        if (d[ny][nx] != INF) continue;
        d[ny][nx] = d[y][x] + (c[ny][nx] == -2 || c[ny][nx] == -1 ? 0 : 1);
        q.push(P(ny, nx));
      }
    }
    return d[gy][gx];
  };
  ll ans = 0;
  for (int _ = (0); _ < (n * n); ++_) {
    int p;
    cin >> p;
    p--;
    int sy = p / n + 1;
    int sx = p % n + 1;
    int res = INF;
    for (int j = 1; j < n + 1; ++j) {
      int gy = 0;
      int gx = j;
      int d = bfs(sy, sx, gy, gx);
      chmin(res, d);
    }
    for (int j = 1; j < n + 1; ++j) {
      int gy = n + 1;
      int gx = j;
      int d = bfs(sy, sx, gy, gx);
      chmin(res, d);
    }
    for (int i = 1; i < n + 1; ++i) {
      int gy = i;
      int gx = 0;
      int d = bfs(sy, sx, gy, gx);
      chmin(res, d);
    }
    for (int i = 1; i < n + 1; ++i) {
      int gy = i;
      int gx = n + 1;
      int d = bfs(sy, sx, gy, gx);
      chmin(res, d);
    }
    ans += res;
    c[sy][sx] = -2;
  }
  cout << ans << endl;
  return 0;
}
