#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <typename T>
void debug(vector<vector<T>>& v, long long h, long long w) {
  for (long long i = 0; i < h; i++) {
    cerr << v[i][0];
    for (long long j = 1; j < w; j++) cerr << " " << v[i][j];
    cerr << endl;
  }
};
template <typename T>
void debug(vector<T>& v, long long n) {
  if (n != 0) cerr << v[0];
  for (long long i = 1; i < n; i++) cerr << " " << v[i];
  cerr << endl;
};
string num2bit(long long num, long long len) {
  string bit = "";
  for (long long i = 0; i < (long long)len; i++) {
    bit += char('0' + (num >> i & 1));
  }
  return bit;
}
const long long INF = (1ll << 60);
vector<long long> dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  vector<long long> A(N * N);
  vector<vector<long long>> D(N, vector<long long>(N, INF));
  vector<vector<long long>> H(N, vector<long long>(N, 1));
  for (long long i = 0; i < (long long)N * N; i++) cin >> A[i];
  for (long long i = 0; i < (long long)N * N; i++) A[i]--;
  for (long long i = 0; i < (long long)N; i++) {
    for (long long j = 0; j < (long long)N; j++) {
      vector<long long> vals = {j, N - 1 - j, i, N - 1 - i};
      for (long long k = 0; k < (long long)4; k++) {
        chmin(D[i][j], vals[k]);
      }
    }
  }
  long long res = 0;
  for (long long i = 0; i < (long long)N * N; i++) {
    long long r = A[i] / N;
    long long c = A[i] % N;
    res += D[r][c];
    H[r][c]--;
    queue<pair<long long, long long>> qu;
    qu.push({r, c});
    while (!qu.empty()) {
      auto now = qu.front();
      qu.pop();
      long long nr = now.first;
      long long nc = now.second;
      for (long long d = 0; d < (long long)4; d++) {
        long long er = nr + dx[d];
        long long ec = nc + dy[d];
        if (er < 0 or er > N - 1 or ec < 0 or ec > N - 1) continue;
        if (D[er][ec] > D[nr][nc] + H[nr][nc]) {
          D[er][ec] = D[nr][nc];
          qu.push({er, ec});
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
