#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
const int inf = 1001001001;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline T powerM(T a, T b) {
  if (b == 0) return 1;
  T tmp = powerM(a, b / 2);
  if (b % 2 == 0)
    return tmp * tmp % MOD;
  else
    return tmp * tmp % MOD * a % MOD;
}
template <class T>
inline T power(T a, T b, T m) {
  if (b == 0) return 1;
  T tmp = power(a, b / 2, m);
  if (b % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * a % m;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
inline T extgcd(T a, T b, T& x, T& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
void hey() { cout << "hey" << endl; }
template <class T>
struct edge {
  int to;
  T cost;
};
vector<vector<edge<int>>> G;
int N;
vector<vector<int>> field, dist;
int main() {
  cin >> N;
  field.assign(N, vector<int>(N, 0));
  for (int i = 1; i <= N * N; i++) {
    int p;
    cin >> p;
    p--;
    field[p / N][p % N] = i;
  }
  dist.assign(N, vector<int>(N, 1001000000));
  for (int i = 0; i < (int)(N); i++) {
    dist[0][i] = 0;
    dist[N - 1][i] = 0;
    dist[i][0] = 0;
    dist[i][N - 1] = 0;
  }
  deque<pair<int, int>> que;
  que.push_front({0, 0});
  while (!que.empty()) {
    int ii, jj;
    tie(ii, jj) = que.front();
    que.pop_front();
    for (int k = 0; k < x4.size(); k++) {
      int ni = ii + y4[k];
      int nj = jj + x4[k];
      if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;
      int wall = field[ii][jj] < field[ni][nj] ? 0 : 1;
      if (wall == 1) {
        if (dist[ni][nj] <= dist[ii][jj] + 1) continue;
        dist[ni][nj] = dist[ii][jj] + 1;
        que.push_back({ni, nj});
      } else {
        if (dist[ni][nj] <= dist[ii][jj]) continue;
        dist[ni][nj] = dist[ii][jj];
        que.push_front({ni, nj});
      }
    }
  }
  int res = 0;
  for (int i = 0; i < (int)(N); i++) {
    for (int j = 0; j < (int)(N); j++) {
      res += dist[i][j];
    }
  }
  cout << res << endl;
}
