#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using vpii = vector<pii>;
using plili = pair<lint, lint>;
using vplili = vector<plili>;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvli = vector<vli>;
using vvb = vector<vb>;
using vvc = vector<vc>;
using vvs = vector<vs>;
using mii = map<int, int>;
using mili = map<int, lint>;
using mci = map<char, int>;
using mcli = map<char, lint>;
using msi = map<string, int>;
using msli = map<string, lint>;
template <class T, class Q>
lint gcd_(T a, Q b) {
  int t;
  while (b != 0) {
    t = a % b;
    a = b;
    b = t;
  }
  return a;
}
template <class T, class Q>
lint lcm_(T a, Q b) {
  return a * b / gcd_(a, b);
}
double distance(pii a, pii b) {
  double dist;
  dist = sqrt((a.first - b.first) * (a.first - b.first) +
              (a.second - b.second) * (a.second - b.second));
  return dist;
}
lint perm(int a) {
  lint perm = 1;
  for (int i = a; i >= 1; i--) {
    perm *= i;
  }
  return perm;
}
lint combination(int n, int m) {
  long double c = 1;
  for (int i = n, k = 1; i > m; i--, k++) {
    c *= i;
    c /= k;
  }
  return (lint)c;
}
template <class T, class Q>
inline bool chmin(T& a, Q b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class Q>
inline bool chmax(T& a, Q b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const double pi = acos(-1);
const int mod = 1000000007;
const int MOD = 998244353;
const int inf = 1045141919;
const lint linf = ((1LL << 62) - 1);
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N;
  cin >> N;
  int size = N * N;
  vb isSat(size, true);
  vi bangou(size);
  vvi graph(size);
  for (int i = 0; i < (int)(N); i++) {
    for (int j = 0; j < (int)(N); j++) {
      cin >> bangou[i * N + j];
      if (i != 0) graph[i * N + j].push_back((i - 1) * N + j);
      if (j != 0) graph[i * N + j].push_back(i * N + j - 1);
      if (i != N - 1) graph[i * N + j].push_back((i + 1) * N + j);
      if (j != N - 1) graph[i * N + j].push_back(i * N + j + 1);
    }
  }
  lint tot_dist = 0;
  for (int i = 0; i < (int)(N); i++) {
    for (int j = 0; j < (int)(N); j++) {
      vi dist(size, -1);
      deque<int> que;
      dist[bangou[i * N + j] - 1] = 0;
      que.push_back(bangou[i * N + j] - 1);
      isSat[bangou[i * N + j] - 1] = false;
      while (!que.empty()) {
        int v = que.front();
        que.pop_front();
        for (auto x : graph[v]) {
          if (dist[x] != -1) continue;
          if (!isSat[x]) {
            que.push_front(x);
            dist[x] = dist[v];
          } else {
            que.push_back(x);
            dist[x] = dist[v] + 1;
          }
          if ((x <= N - 1 && x >= 0) || x % N == 0 || x % N == 1 ||
              (x <= N * N - 1 && x >= N * N - N + 1)) {
            tot_dist += dist[x];
            goto here;
          }
        }
      }
    here : {}
    }
  }
  cout << tot_dist - 4 << endl;
}
