#include <bits/stdc++.h>
using namespace std;
ostream &operator<<(ostream &out, string str) {
  for (char c : str) out << c;
  return out;
}
template <class L, class R>
ostream &operator<<(ostream &out, pair<L, R> p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto &e : x) out << e << (&e == &*--x.end() ? "" : ", ");
  return out << '}';
}
template <class... Args>
void dump(Args &&...args) {
  ((cerr << args << ";  "), ...);
}
template <class T>
int size(T &&a) {
  return (int)a.size();
}
using LL = long long;
using PII = pair<int, int>;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int q = n * n;
  vector<int> p(q);
  for (int i = 0; i < q; i++) cin >> p[i], p[i]--;
  int inf = 1e9;
  vector<int> dst(q + 1, inf), w(q, 1);
  priority_queue<PII> Q;
  auto is_edge = [&](int i) {
    return (i < n || i % n == 0 || i % n == (n - 1) || q - n <= i);
  };
  for (int i = 0; i < q; i++) {
    if (is_edge(i)) {
      dst[i] = 1;
      Q.emplace(-dst[i], i);
    }
  }
  auto bfs = [&]() {
    while (!Q.empty()) {
      int v = Q.top().second;
      Q.pop();
      for (int d : {-1, +1, -n, +n}) {
        int u = v + d;
        if (0 <= u && u < q) {
          if (dst[u] > dst[v] + w[u]) {
            dst[u] = dst[v] + w[u];
            Q.emplace(-dst[u], u);
          }
        }
      }
    }
  };
  bfs();
  LL ans = 0;
  for (int i = 0; i < q; i++) {
    if (is_edge(p[i])) {
      dst[p[i]] = 0;
      Q.emplace(0, p[i]);
    } else
      for (int d : {-1, +1, -n, +n}) {
        int u = p[i] + d;
        if (0 <= u && u < q) Q.emplace(-dst[u], u);
      }
    w[p[i]] = 0;
    bfs();
    ans += dst[p[i]];
  }
  cout << ans << "\n";
}
