#include <bits/stdc++.h>
using namespace std;
int b[250001], n, c[250001], used[250001];
vector<int> a[250001];
int main() {
  cin >> n;
  int nn = pow(n, 2);
  for (int i = 1; i <= nn; i++) {
    if (i >= n + 1) a[i].push_back(i - n);
    if (i + n <= nn) a[i].push_back(i + n);
    if ((i - 1) % n) a[i].push_back(i - 1);
    if (i % n) a[i].push_back(i + 1);
    int u = (i - 1) % n, v = (i - 1) / n;
    c[i] = min(min(u, n - 1 - u), min(v, n - 1 - v));
  }
  int ans = 0;
  for (int i = 1; i <= nn; i++) {
    int y;
    cin >> y;
    ans += c[y];
    used[y] = 1;
    sort(a[y].begin(), a[y].end());
    for (int j = 1; j <= a[y].size() - 1; j++)
      if (a[y][j - 1] == a[y][j]) {
        a[y].erase(a[y].begin() + j - 1);
        j--;
      }
    for (int j = 1; j <= a[y].size(); j++) {
      sort(a[a[y][j - 1]].begin(), a[a[y][j - 1]].end());
      for (int k = 1; k <= a[a[y][j - 1]].size() - 1; k++) {
        if (a[a[y][j - 1]][k] == a[a[y][j - 1]][k - 1]) {
          a[a[y][j - 1]].erase(a[a[y][j - 1]].begin() + k - 1);
          k--;
        }
        if (a[a[y][j - 1]][k - 1] == y)
          a[a[y][j - 1]].erase(a[a[y][j - 1]].begin() + k - 1);
      }
    }
    for (int j = 1; j <= a[y].size(); j++) {
      for (int k = 1; k <= a[y].size(); k++)
        if (a[y][j - 1] != a[y][k - 1]) a[a[y][j - 1]].push_back(a[y][k - 1]);
    }
    queue<int> q;
    q.push(y);
    while (q.size()) {
      int x = q.front();
      q.pop();
      if (used[x])
        for (int i = 1; i <= a[x].size(); i++) {
          if (!used[a[x][i - 1]] && c[a[x][i - 1]] > c[x]) {
            c[a[x][i - 1]] = c[x];
            q.push(a[x][i - 1]);
          }
        }
      else
        for (int i = 1; i <= a[x].size(); i++) {
          if (!used[a[x][i - 1]] && c[a[x][i - 1]] > c[x] + 1) {
            c[a[x][i - 1]] = c[x] + 1;
            q.push(a[x][i - 1]);
          }
        }
    }
  }
  cout << ans;
  return 0;
}
