#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> ps;
  long long n_pow = n * n;
  for (auto i = 0; i < n_pow; i++) {
    long long p;
    cin >> p;
    ps.push_back(p);
  }
  vector<vector<long long>> mat(n + 2, vector<long long>(n + 2, 0));
  long long mat_len = n + 2;
  for (long long x = 0; x < mat_len; x++) {
    for (long long y = 0; y < mat_len; y++) {
      mat[x][y] = min(min(x - 1, mat_len - x - 2), min(y - 1, mat_len - y - 2));
    }
  }
  set<long long> called;
  auto print_mat = [&mat, mat_len]() {
    for (long long x = 0; x < mat_len; x++) {
      for (long long y = 0; y < mat_len; y++) {
        printf("%2lld", mat[y][x]);
      }
      cout << endl;
    }
  };
  auto update_mat = [&mat, mat_len, &called, n](long long mat_x,
                                                long long mat_y) {
    queue<pair<long long, long long>> q;
    set<pair<long long, long long>> visited;
    int patterns[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    long long curmin = 100000000;
    for (auto i = 0; i < 4; i++) {
      auto pattern = patterns[i];
      curmin = min(curmin, mat[mat_x + pattern[0]][mat_y + pattern[1]]);
      auto pp = make_pair(mat_x + pattern[0], mat_y + pattern[1]);
      if (pp.first >= 1 && pp.first <= mat_len - 1 && pp.second >= 1 &&
          pp.second <= mat_len - 1) {
        q.push(pp);
        visited.insert(pp);
      } else {
      }
    }
    mat[mat_x][mat_y] = curmin;
    auto p = make_pair(mat_x, mat_y);
    visited.insert(p);
    while (!q.empty()) {
      auto size = q.size();
      for (auto i = 0; i < size; i++) {
        auto p = q.front();
        q.pop();
        if (p.first == 0 || p.first == mat_len - 1 || p.second == 0 ||
            p.second == mat_len - 1) {
          continue;
        }
        long long cur_min = 100000000;
        for (auto j = 0; j < 4; j++) {
          auto pattern = patterns[j];
          if ((p.first + pattern[0]) >= 0 &&
              (p.first + pattern[0]) <= mat_len - 1 &&
              (p.second + pattern[1]) >= 0 &&
              (p.second + pattern[1]) <= mat_len - 1) {
            cur_min =
                min(cur_min, mat[p.first + pattern[0]][p.second + pattern[1]]);
          }
        }
        long long number = p.first + n * (p.second - 1);
        mat[p.first][p.second] =
            called.count(number) > 0 ? cur_min : cur_min + 1;
        for (auto j = 0; j < 4; j++) {
          auto pattern = patterns[j];
          auto pp = make_pair(p.first + pattern[0], p.second + pattern[1]);
          if (visited.count(pp) == 0 && pp.first >= 1 &&
              pp.first < mat_len - 1 && pp.second >= 1 &&
              pp.second < mat_len - 1) {
            q.push(pp);
            visited.insert(pp);
          }
        }
      }
    }
    return;
  };
  long long cnt = 0;
  for (auto i = 0; i < ps.size(); i++) {
    auto p = ps[i];
    called.insert(p);
    long long mat_x = (p - 1) % n + 1;
    long long mat_y = ((p - 1) / n) + 1;
    cnt += mat[mat_x][mat_y];
    update_mat(mat_x, mat_y);
  }
  cout << cnt << endl;
  return 0;
}
