#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
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;
}
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++) par[i] = i;
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int N, ans = 0, cnt = 0;
  cin >> N;
  UnionFind tree(N * N + 1);
  int MN[(N + 1) * (N + 1)];
  bool CAN[N + 2][N + 2];
  for (int i = 0; i <= N + 1; ++i) {
    for (int j = 0; j <= N + 1; ++j) {
      CAN[i][j] = false;
      MN[i * N + j] = 5100000;
    }
  }
  for (int i = 1; i <= N * N; ++i) {
    int P;
    cin >> P;
    cnt = 5100000;
    int Y = (P + N - 1) / N;
    int X = (P - 1) % N + 1;
    chmin(cnt, X - 1);
    chmin(cnt, N - X);
    chmin(cnt, Y - 1);
    chmin(cnt, N - Y);
    MN[(Y - 1) * N + X] = cnt;
    for (int i = 0; i < 4; ++i) {
      int ny = Y + dy[i], nx = X + dx[i];
      if (nx < 1 || nx > N || ny < 1 || ny > N || CAN[ny][nx] == false)
        continue;
      chmin(MN[(Y - 1) * N + X], MN[tree.root((ny - 1) * N + nx)]);
      chmin(MN[tree.root((ny - 1) * N + nx)], MN[(Y - 1) * N + X]);
      tree.unite((ny - 1) * N + nx, (Y - 1) * N + X);
    }
    CAN[Y][X] = true;
    ans += MN[(Y - 1) * N + X];
  }
  cout << ans << '\n';
  return 0;
}
