#include <bits/stdc++.h>
using namespace std;
long n, ans = 0;
vector<long> p(251000);
vector<bool> ng(251000, 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(251000, true);
    priority_queue<long> aaa;
    aaa.push(100000000000000 + p[i] - 1);
    mitan[p[i] - 1] = false;
    while (m_ans == -1) {
      vector<long> x = {1000000 - aaa.top() / 100000000, aaa.top() % 100000000};
      if (x[1] < n) m_ans = x[0];
      if (n * n - x[1] < n) m_ans = x[0];
      if (x[1] % n == 0) m_ans = x[0];
      if (x[1] % n == n - 1) m_ans = x[0];
      if (m_ans == -1) {
        if (mitan[x[1] + 1] && ng[x[1] + 1])
          aaa.push(100000000000000 - (x[0] + 1) * 100000000 + x[1] + 1);
        if (mitan[x[1] + 1] && !(ng[x[1] + 1]))
          aaa.push(100000000000000 - x[0] * 100000000 + x[1] + 1);
        if (mitan[x[1] + 1]) mitan[x[1] + 1] = false;
        if (mitan[x[1] - 1] && ng[x[1] - 1])
          aaa.push(100000000000000 - (x[0] + 1) * 100000000 + x[1] - 1);
        if (mitan[x[1] - 1] && !(ng[x[1] - 1]))
          aaa.push(100000000000000 - x[0] * 100000000 + x[1] - 1);
        if (mitan[x[1] - 1]) mitan[x[1] - 1] = false;
        if (mitan[x[1] + n] && ng[x[1] + n])
          aaa.push(100000000000000 - (x[0] + 1) * 100000000 + x[1] + n);
        if (mitan[x[1] + n] && !(ng[x[1] + n]))
          aaa.push(100000000000000 - x[0] * 100000000 + x[1] + n);
        if (mitan[x[1] + n]) mitan[x[1] + n] = false;
        if (mitan[x[1] - n] && ng[x[1] - n])
          aaa.push(100000000000000 - (x[0] + 1) * 100000000 + x[1] - n);
        if (mitan[x[1] - n] && !(ng[x[1] - n]))
          aaa.push(100000000000000 - x[0] * 100000000 + 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;
}
