#include <bits/stdc++.h>
template <typename T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long int;
const int MOD = 1000000007;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;
ll N;
template <typename T>
class my_PQ {
 public:
  vector<T> q;
  my_PQ() {}
  my_PQ(ll N) { q.resize(N); }
  my_PQ(ll N, T a) {
    q.resize(N);
    for (ll i = 0; i < N; i++) q[i] = a;
  }
  size_t size() { return q.size(); }
  typename vector<T>::iterator begin() { return q.begin(); }
  typename vector<T>::iterator end() { return q.end(); }
  void resize(ll N) { q.resize(N); }
  T& operator[](ll n) & { return q[n]; }
  const T& operator[](ll n) const& { return q[n]; }
  void push_back(T&& x) { q.push_back(x); }
  void clear() { q.clear(); }
  void erase(ll i) { q.erase(q.begin() + i); }
  ll insert(T x, bool (*comp)(T a, T b)) {
    ll left = -1;
    ll right = (ll)q.size();
    while (right - left > 1) {
      ll mid = left + (right - left) / 2;
      if (!comp(q[mid], x))
        right = mid;
      else
        left = mid;
    }
    q.insert(q.begin() + right, x);
    return right;
  }
};
bool comp(vector<ll> a, vector<ll> b) { return a[2] < b[2]; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<vector<ll>> P(N, vector<ll>(N));
  for (ll i = 0; i < N * N; i++) {
    ll a;
    cin >> a;
    a--;
    P[a % N][a / N] = i;
  }
  ll ans = 0;
  vector<vector<ll>> score(N, vector<ll>(N, -1));
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      my_PQ<vector<ll>> open;
      open.push_back(vector<ll>{i, j, 0});
      vector<vector<bool>> done(N, vector<bool>(N, false));
      done[i][j] = true;
      while (open.size() > 0) {
        vector<ll> node = open[0];
        if (node[2] == 0 && score[node[0]][node[1]] == 0) {
          score[i][j] = 0;
          break;
        }
        if (node[0] == 0 || node[0] == N - 1 || node[1] == 0 ||
            node[1] == N - 1) {
          ans += node[2];
          score[i][j] = node[2];
          break;
        }
        open.erase(+0);
        if (!done[node[0] + 1][node[1]]) {
          if (P[node[0] + 1][node[1]] > P[i][j])
            open.insert(vector<ll>{node[0] + 1, node[1], node[2] + 1}, comp);
          else
            open.insert(vector<ll>{node[0] + 1, node[1], node[2]}, comp);
          done[node[0] + 1][node[1]] = true;
        }
        if (!done[node[0] - 1][node[1]]) {
          if (P[node[0] - 1][node[1]] > P[i][j])
            open.insert(vector<ll>{node[0] - 1, node[1], node[2] + 1}, comp);
          else
            open.insert(vector<ll>{node[0] - 1, node[1], node[2]}, comp);
          done[node[0] - 1][node[1]] = true;
        }
        if (!done[node[0]][node[1] + 1]) {
          if (P[node[0]][node[1] + 1] > P[i][j])
            open.insert(vector<ll>{node[0], node[1] + 1, node[2] + 1}, comp);
          else
            open.insert(vector<ll>{node[0], node[1] + 1, node[2]}, comp);
          done[node[0]][node[1] + 1] = true;
        }
        if (!done[node[0]][node[1] - 1]) {
          if (P[node[0]][node[1] - 1] > P[i][j])
            open.insert(vector<ll>{node[0], node[1] - 1, node[2] + 1}, comp);
          else
            open.insert(vector<ll>{node[0], node[1] - 1, node[2]}, comp);
          done[node[0]][node[1] - 1] = true;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
