#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double Pi = acos(-1);
int main() {
  long long N;
  cin >> N;
  vector<long long> P(N * N);
  for (long long i = 0; i < N * N; i++) {
    cin >> P.at(i);
  }
  using Graph = vector<vector<long long>>;
  Graph G(N * N);
  for (long long i = 0; i < N * N; ++i) {
    if (i % N != N - 1) {
      G[i].push_back(i + 1);
      G[i + 1].push_back(i);
    }
    if (i < N * (N - 1)) {
      G[i].push_back(i + N);
      G[i + N].push_back(i);
    }
  }
  long long answer = 0;
  vector<long long> ans(N * N);
  for (long long i = 0; i < N * N; i++) {
    ans.at(P.at(i) - 1) = 1000000;
    vector<long long> dist(N * N);
    vector<bool> tf(N * N);
    queue<long long> que;
    dist[P.at(i) - 1] = 0;
    que.push(P.at(i) - 1);
    if ((P.at(i) - 1) < N || (P.at(i) - 1) > N * N - N - 1 ||
        (P.at(i) - 1) % N == 0 || (P.at(i) - 1) % N == N - 1) {
      ans.at(P.at(i) - 1) = 0;
      long long value_to_remove = P.at(i) - 1;
      G[P.at(i) - 1].erase(G[P.at(i) - 1].begin(), G[P.at(i) - 1].end());
      if (P.at(i) - 1 < N * (N - 1)) {
        G[P.at(i) - 1 + N].erase(
            std::remove(G[P.at(i) - 1 + N].begin(), G[P.at(i) - 1 + N].end(),
                        value_to_remove),
            G[P.at(i) - 1 + N].end());
      }
      if (P.at(i) - 1 >= N) {
        G[P.at(i) - 1 - N].erase(
            std::remove(G[P.at(i) - 1 - N].begin(), G[P.at(i) - 1 - N].end(),
                        value_to_remove),
            G[P.at(i) - 1 - N].end());
      }
      if ((P.at(i) - 1) % N != 0) {
        G[P.at(i) - 1 - 1].erase(
            std::remove(G[P.at(i) - 1 - 1].begin(), G[P.at(i) - 1 - 1].end(),
                        value_to_remove),
            G[P.at(i) - 1 - 1].end());
      }
      if ((P.at(i) - 1) % N != N - 1) {
        G[P.at(i) - 1 + 1].erase(
            std::remove(G[P.at(i) - 1 + 1].begin(), G[P.at(i) - 1 + 1].end(),
                        value_to_remove),
            G[P.at(i) - 1 + 1].end());
      }
      tf.at(P.at(i) - 1) = true;
      continue;
    }
    while (!que.empty()) {
      long long w = que.front();
      que.pop();
      for (long long nw : G[w]) {
        if (dist[nw] != 0) continue;
        if (nw < N || nw > N * N - N - 1 || nw % N == 0 || nw % N == N - 1) {
        }
        dist[nw] = dist[w] + 1;
        if (tf.at(w) == true) {
          dist[nw]--;
        }
        que.push(nw);
      }
      for (long long j = 0; j < N * N; j++) {
        if (dist[j] != -1 && P.at(i) - 1 != j && tf.at(j) != true)
          ans.at(P.at(i) - 1) = min(ans.at(P.at(i) - 1), dist[j]);
      }
      if (P.at(i) - 1 >= N && (P.at(i) - 1) <= N * N - N - 1 &&
          (P.at(i) - 1) % N != 0 && (P.at(i) - 1) % N != N - 1) {
        long long value_to_remove = P.at(i) - 1;
        if (P.at(i) - 1 < N * (N - 1)) {
          G[(P.at(i) - 1) + N].erase(
              std::remove(G[P.at(i) - 1 + N].begin(), G[P.at(i) - 1 + N].end(),
                          value_to_remove),
              G[P.at(i) - 1 + N].end());
        }
        if (P.at(i) - 1 >= N) {
          G[(P.at(i) - 1) - N].erase(
              std::remove(G[P.at(i) - 1 - N].begin(), G[P.at(i) - 1 - N].end(),
                          value_to_remove),
              G[P.at(i) - 1 - N].end());
        }
        if ((P.at(i) - 1) % N != 0) {
          G[(P.at(i) - 1) - 1].erase(
              std::remove(G[P.at(i) - 1 - 1].begin(), G[P.at(i) - 1 - 1].end(),
                          value_to_remove),
              G[P.at(i) - 1 - 1].end());
        }
        if ((P.at(i) - 1) % N != N - 1) {
          G[(P.at(i) - 1) + 1].erase(
              std::remove(G[P.at(i) - 1 + 1].begin(), G[P.at(i) - 1 + 1].end(),
                          value_to_remove),
              G[P.at(i) - 1 + 1].end());
        }
      }
    }
    for (long long j = 0; j < N * N; j++) {
    }
    tf.at(P.at(i) - 1) = true;
    answer += ans.at(P.at(i) - 1);
  }
  cout << answer << endl;
}
