#include <bits/stdc++.h>
using namespace std;
const int sz = 503;
int n;
vector<int> p;
int g[sz][sz];
int ok[sz][sz];
pair<int, int> pred[sz][sz];
void bfs() {
  set<pair<int, pair<int, int>>> q;
  for (int i = 0; i < n + 2; i++) {
    q.insert({0, {i, 0}});
    q.insert({0, {i, n + 1}});
    q.insert({0, {0, i}});
    q.insert({0, {n + 1, i}});
  }
  while (q.size()) {
    pair<int, pair<int, int>> u = *q.begin();
    q.erase(q.begin());
    int x = u.second.first, y = u.second.second;
    if (x < 0 || x > n + 1 || y < 0 || y > n + 1 || pred[x][y].first != -1)
      continue;
    pred[x][y] = {x, y};
    g[x][y] = u.first;
    q.insert({u.first + 1, {x + 1, y}});
    q.insert({u.first + 1, {x - 1, y}});
    q.insert({u.first + 1, {x, y + 1}});
    q.insert({u.first + 1, {x, y - 1}});
  }
}
int upd(int x, int y) {
  int val = g[x][y];
  ok[x][y] = 0;
  set<pair<int, pair<int, int>>> q;
  q.insert({val - 1, {x, y}});
  while (q.size()) {
    pair<int, pair<int, int>> u = *q.begin();
    q.erase(q.begin());
    int x = u.second.first, y = u.second.second;
    g[x][y] = u.first;
    if (x > 0 && x + 1 < n && y > 0 && y < n &&
        g[x + 1][y] > u.first + ok[x + 1][y])
      q.insert({u.first + ok[x + 1][y], {x + 1, y}});
    if (x - 1 > 0 && x < n && y > 0 && y < n &&
        g[x - 1][y] > u.first + ok[x - 1][y])
      q.insert({u.first + ok[x - 1][y], {x - 1, y}});
    if (x > 0 && x < n && y > 0 && y + 1 < n &&
        g[x][y + 1] > u.first + ok[x][y + 1])
      q.insert({u.first + ok[x][y + 1], {x, y + 1}});
    if (x > 0 && x < n && y > 0 && y - 1 < n &&
        g[x][y - 1] > u.first + ok[x][y - 1])
      q.insert({u.first + ok[x][y - 1], {x, y - 1}});
  }
  return val;
}
inline void solve() {
  cin >> n;
  p.resize(n * n);
  for (int i = 0; i < n * n; i++) cin >> p[i];
  for (int i = 0; i < n + 2; i++)
    for (int j = 0; j < n + 2; j++) {
      ok[i][j] = 1;
      pred[i][j] = {-1, -1};
    }
  bfs();
  int otv = 0;
  for (int i = 0; i < n * n; i++) {
    int x = p[i] / n + (p[i] % n > 0 ? 1 : 0);
    int y = (p[i] % n > 0 ? p[i] % n : n);
    otv += upd(x, y) - 1;
  }
  cout << otv;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  for (; q > 0; q--) {
    solve();
    cout << '\n';
  }
}
