#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int bfs(vector<vector<int>> vv, int p, int N) {
  if (p / N == 0 || p / N == N - 1 || p % N == 0 || p % N == N - 1) {
    return 0;
  }
  vector<vector<int>> vt(N, vector<int>(N));
  queue<int> Q;
  Q.push(p);
  vt.at(p / N).at(p % N) = 1;
  int r = 250;
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    int c = vt.at(t / N).at(t % N);
    if (t / N != 0) {
      if (vt.at(t / N - 1).at(t % N) == 0) {
        vt.at(t / N - 1).at(t % N) = c + vv.at(t / N - 1).at(t % N);
        if (t / N - 1 == 0) {
          chmin(r, vt.at(t / N - 1).at(t % N));
          if (r == 1) break;
        } else
          Q.push(t - N);
      }
    }
    if (t / N != N - 1) {
      if (vt.at(t / N + 1).at(t % N) == 0) {
        vt.at(t / N + 1).at(t % N) = c + vv.at(t / N + 1).at(t % N);
        if (t / N + 1 == N) {
          chmin(r, vt.at(t / N + 1).at(t % N));
          if (r == 1) break;
        } else
          Q.push(t + N);
      }
    }
    if (t % N != 0) {
      if (vt.at(t / N).at(t % N - 1) == 0) {
        vt.at(t / N).at(t % N - 1) = c + vv.at(t / N).at(t % N - 1);
        if (t % N - 1 == 0) {
          chmin(r, vt.at(t / N).at(t % N - 1));
          if (r == 1) break;
        } else
          Q.push(t - 1);
      }
    }
    if (t % N != N - 1) {
      if (vt.at(t / N).at(t % N + 1) == 0) {
        vt.at(t / N).at(t % N + 1) = c + vv.at(t / N).at(t % N + 1);
        if (t % N + 1 == N) {
          chmin(r, vt.at(t / N).at(t % N + 1));
          if (r == 1) break;
        } else
          Q.push(t - N);
      }
    }
  }
  return r - 1;
}
int main() {
  int N;
  cin >> N;
  vector<int> v(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> v.at(i);
    v.at(i)--;
  }
  int a = 0, b = 0, c = 0, x, y, z;
  vector<vector<int>> vv(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      vv.at(i).at(j) = 1;
    }
  }
  for (int i = 0; i < N * N; i++) {
    a += bfs(vv, v.at(i), N);
    vv.at(v.at(i) / N).at(v.at(i) % N) = 0;
  }
  std::cout << a << '\n';
}
