#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
const long long mod = 1e9 + 7;
template <class T>
inline void chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T>
inline void chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<bool> sit(N * N + 1, 1);
  long long count = 0;
  vector<int> who(N * N);
  for (long long(i) = 0; (i) < (N * N); (i)++) cin >> who[i];
  for (long long(i) = 0; (i) < (N * N); (i)++) {
    vector<int> seen(N * N + 1, 0);
    queue<int> que;
    queue<int> next;
    que.push(who[i]);
    long long mindist = INT32_MAX;
    sit[who[i]] = 0;
    if (who[i] <= N) {
      continue;
    }
    if (who[i] > N * (N - 1)) {
      continue;
    }
    if (who[i] % N == 1) {
      continue;
    }
    if (who[i] % N == 0) {
      continue;
    }
    for (long long l = 0;; l++) {
      while (!que.empty()) {
        int whe = que.front();
        que.pop();
        seen[whe] = 1;
        if (whe <= 2 * N) {
          chmin(mindist, l + sit[whe - N]);
        } else {
          if (seen[whe - N] != 1) {
            if (sit[whe - N] == 1) {
              next.push(whe - N);
            } else {
              que.push(whe - N);
            }
          }
        }
        if (whe > N * (N - 2)) {
          chmin(mindist, l + sit[whe + N]);
        } else {
          if (seen[whe + N] != 1) {
            if (sit[whe + N] == 1) {
              next.push(whe + N);
            } else {
              que.push(whe + N);
            }
          }
        }
        if (whe % N == 2) {
          chmin(mindist, l + sit[whe - 1]);
        } else {
          if (seen[whe - 1] != 1) {
            if (sit[whe - 1] == 1) {
              next.push(whe - 1);
            } else {
              que.push(whe - 1);
            }
          }
        }
        if (whe % N == N - 1) {
          chmin(mindist, l + sit[whe + 1]);
        } else {
          if (seen[whe + 1] != 1) {
            if (sit[whe + 1] == 1) {
              next.push(whe + 1);
            } else {
              que.push(whe + 1);
            }
          }
        }
        if (mindist == l) break;
      }
      swap(que, next);
      if (mindist != INT32_MAX) break;
    }
    count += mindist;
  }
  cout << count << "\n";
}
