#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct PP {
  int cost;
  int x;
  int y;
  PP(int c, int x, int y) : cost(c), x(x), y(y) {}
  bool operator<(const PP& a) const { return this->cost > a.cost; }
};
int bfs(const vector<vector<bool>>& tab, int x0, int y0) {
  const int n = tab.size();
  priority_queue<PP> que;
  que.push(PP(0, x0, y0));
  set<P> s;
  while (!que.empty()) {
    PP t = que.top();
    que.pop();
    int cost = t.cost;
    int x = t.x;
    int y = t.y;
    if (s.count(P(x, y))) continue;
    s.insert(P(x, y));
    if (x <= 0) return cost;
    if (x >= n - 1) return cost;
    if (y <= 0) return cost;
    if (y >= n - 1) return cost;
    if (tab[y][x - 1]) {
      que.push(PP(cost + 1, x - 1, y));
    } else {
      que.push(PP(cost, x - 1, y));
    }
    if (tab[y][x + 1]) {
      que.push(PP(cost + 1, x + 1, y));
    } else {
      que.push(PP(cost, x + 1, y));
    }
    if (tab[y - 1][x]) {
      que.push(PP(cost + 1, x, y - 1));
    } else {
      que.push(PP(cost, x, y - 1));
    }
    if (tab[y + 1][x]) {
      que.push(PP(cost + 1, x, y + 1));
    } else {
      que.push(PP(cost, x, y + 1));
    }
  }
  return -1;
}
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < (n * n); ++i) cin >> p[i];
  vector<vector<bool>> tab(n, vector<bool>(n, true));
  ll res = 0;
  for (int i = 0; i < (n * n); ++i) {
    int x = (p[i] - 1) % n;
    int y = (p[i] - 1) / n;
    int c = bfs(tab, x, y);
    res += c;
    tab[y][x] = false;
  }
  cout << res << endl;
  return 0;
}
