#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long maxn = 5e2 + 100;
long long n, m, k, t;
bool lefft[maxn][maxn];
bool used[maxn][maxn];
vector<pair<long long, long long> > go{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool in(long long first, long long second) {
  return first >= 0 && second >= 0 && first < n && second < n;
}
pair<long long, long long> get_coord(long long c) {
  long long first = c / n, second = c % n;
  return make_pair(first, second);
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q, w, e, a, b, c;
  cin >> n;
  vector<long long> A(n * n);
  for (q = 0; q < n * n; q++) {
    cin >> A[q];
    A[q]--;
  }
  long long ans = 0;
  for (q = 0; q < n * n; q++) {
    deque<pair<pair<long long, long long>, long long> > D;
    pair<long long, long long> p = get_coord(A[q]);
    long long first = p.first, second = p.second;
    lefft[first][second] = 1;
    D.push_back(make_pair(make_pair(first, second), 0));
    vector<pair<long long, long long> > unuse;
    long long dans = 15;
    while (!D.empty()) {
      pair<pair<long long, long long>, long long> p = D.front();
      D.pop_front();
      long long first = p.first.first;
      long long second = p.first.second;
      used[first][second] = 1;
      unuse.push_back(make_pair(first, second));
      long long d = p.second;
      bool f = 0;
      for (auto p1 : go) {
        long long dx = p1.first, dy = p1.second;
        long long nx = first + dx, ny = second + dy;
        if (!in(nx, ny)) {
          dans = d;
          f = 1;
          break;
        }
        if (used[nx][ny]) {
          continue;
        }
        if (lefft[nx][ny]) {
          pair<pair<long long, long long>, long long> p2 =
              make_pair(make_pair(nx, ny), d);
          D.push_front(p2);
        } else {
          pair<pair<long long, long long>, long long> p2 =
              make_pair(make_pair(nx, ny), d + 1);
          D.push_back(p2);
        }
      }
      if (f) {
        break;
      }
    }
    for (auto p : unuse) {
      used[p.first][p.second] = 0;
    }
    ans += dans;
  }
  cout << ans;
  return 0;
}
