#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct graph {
  int size, last;
  vector<list<int>> adj;
  vector<bool> w;
  graph(int n) : size(n), last(n - 1), adj(n), w(n, true) { w[last] = false; }
  void addEdge(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
};
int bfs(graph& g, int x) {
  vector<bool> visit(g.size, false);
  vector<int> dist(g.size, 0x3f3f3f3f);
  queue<int> q0, q1;
  visit[x] = true;
  dist[x] = 0;
  q0.push(x);
  int u;
  while (!q0.empty() or !q1.empty()) {
    if (q0.empty())
      u = q1.front(), q1.pop();
    else
      u = q0.front(), q0.pop();
    if (u == g.last) {
      g.w[x] = false;
      return dist[u];
    }
    for (auto& v : g.adj[u]) {
      if (visit[v]) continue;
      visit[v] = true;
      dist[v] = dist[u] + g.w[v];
      g.w[v] ? q1.push(v) : q0.push(v);
    }
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  graph g(n * n + 1);
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) {
    g.addEdge(i, i % n ? i - 1 : g.last);
    g.addEdge(i, i >= n ? i - n : g.last);
    if (i % n == n - 1) g.addEdge(i, g.last);
    if (i >= n * (n - 1)) g.addEdge(i, g.last);
    cin >> p[i];
  }
  long long ans = 0;
  for (auto& x : p) {
    ans += (long long)bfs(g, x - 1);
  }
  cout << ans << "\n";
  return 0;
}
