#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 = 5000;
    vector<bool> mitan(251000, true);
    queue<long> aaa;
    aaa.push(100000000000000 + p[i] - 1);
    mitan[p[i] - 1] = false;
    while (aaa.size() != 0) {
      long x0 = 1000000 - aaa.front() / 100000000;
      long x1 = aaa.front() % 100000000;
      aaa.pop();
      if (x0 >= m_ans) continue;
      if (x1 < n) m_ans = x0;
      if (n * n - x1 < n) m_ans = x0;
      if (x1 % n == 0) m_ans = x0;
      if (x1 % n == n - 1) m_ans = x0;
      if (m_ans == x0) continue;
      if (mitan[x1 + 1]) {
        mitan[x1 + 1] = false;
        if (ng[x1 + 1])
          aaa.push(100000000000000 - (x0 + 1) * 100000000 + x1 + 1);
        else
          aaa.push(100000000000000 - x0 * 100000000 + x1 + 1);
      }
      if (mitan[x1 - 1]) {
        mitan[x1 - 1] = false;
        if (ng[x1 - 1])
          aaa.push(100000000000000 - (x0 + 1) * 100000000 + x1 - 1);
        else
          aaa.push(100000000000000 - x0 * 100000000 + x1 - 1);
      }
      if (mitan[x1 + n]) {
        mitan[x1 + n] = false;
        if (ng[x1 + n])
          aaa.push(100000000000000 - (x0 + 1) * 100000000 + x1 + n);
        else
          aaa.push(100000000000000 - x0 * 100000000 + x1 + n);
      }
      if (mitan[x1 - n]) {
        mitan[x1 - n] = false;
        if (ng[x1 - n])
          aaa.push(100000000000000 - (x0 + 1) * 100000000 + x1 - n);
        else
          aaa.push(100000000000000 - x0 * 100000000 + x1 - n);
      }
    }
    ans += m_ans;
    ng[p[i] - 1] = false;
  }
  cout << ans << endl;
}
