#include <bits/stdc++.h>
using namespace std;
long n, ans = 0;
vector<long> p(30000);
vector<bool> ng(30000, true);
int main() {
  cin >> n;
  for (long i = 0; i < n * n; i++) cin >> p[i];
  for (long i = 0; i < n * n; i++) {
    long m_ans = -1;
    vector<bool> mitan(30000, true);
    priority_queue<vector<long>> aaa;
    aaa.push({0, p[i] - 1});
    mitan[p[i] - 1] = false;
    while (m_ans == -1) {
      vector<long> x = aaa.top();
      if (x[1] <= n) m_ans = 0 - x[0];
      if (n * n - x[1] <= n) m_ans = 0 - x[0];
      if (x[1] % n == 0) m_ans = 0 - x[0];
      if (x[1] % n == n - 1) m_ans = 0 - x[0];
      if (m_ans == -1) {
        if (mitan[x[1] + 1] && ng[x[1] + 1]) aaa.push({x[0] - 1, x[1] + 1});
        if (mitan[x[1] + 1] && !(ng[x[1] + 1])) aaa.push({x[0], x[1] + 1});
        if (mitan[x[1] + 1]) mitan[x[1] + 1] = false;
        if (mitan[x[1] - 1] && ng[x[1] - 1]) aaa.push({x[0] - 1, x[1] - 1});
        if (mitan[x[1] - 1] && !(ng[x[1] - 1])) aaa.push({x[0], x[1] - 1});
        if (mitan[x[1] - 1]) mitan[x[1] - 1] = false;
        if (mitan[x[1] + n] && ng[x[1] + n]) aaa.push({x[0] - 1, x[1] + n});
        if (mitan[x[1] + n] && !(ng[x[1] + n])) aaa.push({x[0], x[1] + n});
        if (mitan[x[1] + n]) mitan[x[1] + n] = false;
        if (mitan[x[1] - n] && ng[x[1] - n]) aaa.push({x[0] - 1, x[1] - n});
        if (mitan[x[1] - n] && !(ng[x[1] - n])) aaa.push({x[0], x[1] - n});
        if (mitan[x[1] - n]) mitan[x[1] - n] = false;
      }
      aaa.pop();
    }
    ans += m_ans;
    ng[p[i] - 1] = false;
  }
  cout << ans << endl;
}
