#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1e9 + 7;
long long dx[4] = {-1, 1, 0, 0};
long long dy[4] = {0, 0, -1, 1};
int main() {
  long long n;
  cin >> n;
  vector<long long> p(n * n);
  long long sum = 0;
  vector<vector<pair<long long, long long>>> ans(
      n, vector<pair<long long, long long>>(n, make_pair(0, 1)));
  for (long long i = 0; i < (n * n); i++) {
    cin >> p[i];
    p[i]--;
  }
  for (long long i = 0; i < (n); i++) {
    for (long long j = 0; j < (n); j++) {
      ans[i][j].first = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }
  for (long long k = 0; k < (n * n); k++) {
    long long i = p[k];
    sum += ans[i / n][i % n].first;
    queue<pair<long long, long long>> que;
    que.push(make_pair(i / n, i % n));
    ans[i / n][i % n].second = 0;
    vector<vector<long long>> s(n, vector<long long>(n));
    while (!que.empty()) {
      pair<long long, long long> x = que.front();
      que.pop();
      s[x.first][x.second] = 1;
      if (ans[x.first][x.second].second == 0) {
        for (long long j = 0; j < (4); j++) {
          long long nx = x.first + dx[j];
          long long ny = x.second + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              s[nx][ny] == 0 &&
              ans[nx][ny].first > ans[x.first][x.second].first) {
            ans[nx][ny].first = ans[x.first][x.second].first;
            que.push(make_pair(nx, ny));
          }
        }
      } else {
        for (long long j = 0; j < (4); j++) {
          long long nx = x.first + dx[j];
          long long ny = x.second + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              s[nx][ny] == 0 &&
              ans[nx][ny].first > ans[x.first][x.second].first) {
            ans[nx][ny].first = ans[x.first][x.second].first + 1;
            que.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}
