#include <bits/stdc++.h>
using namespace std;
int64_t solve(int N, const vector<int>& perm) {
  using coord = int;
  constexpr int TBC = INT_MAX;
  constexpr char block = '#';
  const vector<pair<int, int>> diff = {make_pair(0, -1), make_pair(-1, 0),
                                       make_pair(1, 0), make_pair(0, 1)};
  vector<string> board(N, string(N, block));
  int64_t ans = 0;
  for (size_t i = 0; i < perm.size(); i++) {
    int s = perm.at(i);
    int s_x = s / N;
    int s_y = s % N;
    vector<vector<int>> checked(N, vector<int>(N, TBC));
    deque<coord> bfs;
    checked.at(s_x).at(s_y) = 0;
    bfs.push_front(s);
    while (!bfs.empty()) {
      coord p = bfs.front();
      bfs.pop_front();
      int x = p / N;
      int y = p % N;
      bool escaped = false;
      for (const auto& [dx, dy] : diff) {
        if (x + dx <= -1 || x + dx >= N || y + dy <= -1 || y + dy >= N) {
          escaped = true;
          ans += checked.at(x).at(y);
          board.at(s_x).at(s_y) = '.';
          break;
        } else if (board.at(x + dx).at(y + dy) != block &&
                   checked.at(x + dx).at(y + dy) == TBC) {
          checked.at(x + dx).at(y + dy) = checked.at(x).at(y);
          bfs.push_front((x + dx) * N + y + dy);
        } else if (board.at(x + dx).at(y + dy) == block &&
                   checked.at(x + dx).at(y + dy) == TBC) {
          checked.at(x + dx).at(y + dy) = checked.at(x).at(y) + 1;
          bfs.push_back((x + dx) * N + y + dy);
        }
      }
      if (escaped) {
        bfs.clear();
        break;
      }
    }
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  vector<int> perm(N * N);
  for (size_t i = 0; i < perm.size(); i++) {
    cin >> perm.at(i);
    perm.at(i)--;
  }
  int64_t ans = solve(N, perm);
  cout << ans << endl;
}
