#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll count_pair(int n, int np2, int p, vector<int> riseki) {
  vector<int> dp(np2, 1001001001);
  dp[p] = 0;
  P u(dp[p], p);
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(u);
  while (q.size() != 0) {
    P x = q.top();
    int xc = x.first;
    int xn = x.second;
    q.pop();
    if (xn % n != 0) {
      if (dp[xn - 1] > xc) {
        dp[xn - 1] = xc + ((riseki[xn - 1] != 1) ? 1 : 0);
        P tmp(dp[xn - 1], xn - 1);
        q.push(tmp);
      }
    }
    if (xn % n != n - 1) {
      if (dp[xn + 1] > xc) {
        dp[xn + 1] = xc + ((riseki[xn + 1] != 1) ? 1 : 0);
        P tmp(dp[xn + 1], xn + 1);
        q.push(tmp);
      }
    }
    if (xn > n - 1) {
      if (dp[xn - n] > xc) {
        dp[xn - n] = xc + ((riseki[xn - n] != 1) ? 1 : 0);
        P tmp(dp[xn - n], xn - n);
        q.push(tmp);
      }
    }
    if (xn < np2 - n) {
      if (dp[xn + n] > xc) {
        dp[xn + n] = xc + ((riseki[xn + n] != 1) ? 1 : 0);
        P tmp(dp[xn + n], xn + n);
        q.push(tmp);
      }
    }
  }
  ll ans = dp[0];
  for (int i = 0; i < n; i++) {
    if (dp[i] < ans) {
      ans = dp[i];
    }
  }
  for (int i = 0; i < np2; i += n) {
    if (dp[i] < ans) {
      ans = dp[i];
    }
  }
  for (int i = n - 1; i < np2; i += n) {
    if (dp[i] < ans) {
      ans = dp[i];
    }
  }
  for (int i = np2 - n; i < np2; i++) {
    if (dp[i] < ans) {
      ans = dp[i];
    }
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  int np2 = n * n;
  vector<int> p(np2, 0);
  vector<int> riseki(np2, 0);
  for (int i = 0; i < np2; i++) {
    cin >> p[i];
    p[i]--;
  }
  ll cnt = 0;
  for (int i = 0; i < np2; i++) {
    riseki[p[i]] = 1;
    cnt += count_pair(n, np2, p[i], riseki);
  }
  cout << cnt << endl;
}
