#include <bits/stdc++.h>
int main(void) {
  int N;
  int ans = 0;
  std::vector<int> P;
  std::cin >> N;
  std::vector<std::vector<bool> > sit(N, std::vector<bool>(N, true));
  std::vector<std::vector<int> > val(N, std::vector<int>(N, 0));
  std::vector<std::pair<int, int> > next;
  int add;
  for (int i = 0; i < N * N; ++i) {
    int p;
    std::cin >> p;
    P.push_back(p - 1);
  }
  while (P.size() > 0) {
    int p = P.front();
    int row = 0, col = 0;
    P.erase(P.begin());
    while (p >= N) {
      p = p - N;
      row++;
    }
    col = p;
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        val.at(i).at(j) = 100000000;
      }
    }
    val.at(row).at(col) = 0;
    sit.at(row).at(col) = false;
    next.push_back(std::make_pair(row, col));
    while (next.size() > 0) {
      row = next.back().first;
      col = next.back().second;
      next.pop_back();
      if ((row - 1) >= 0) {
        if (sit.at(row - 1).at(col) == true) {
          add = 1;
        } else {
          add = 0;
        }
        if (val.at(row - 1).at(col) > val.at(row).at(col) + add) {
          val.at(row - 1).at(col) = val.at(row).at(col) + add;
          next.push_back(std::make_pair(row - 1, col));
        }
      }
      if ((row + 1) < N) {
        if (sit.at(row + 1).at(col) == true) {
          add = 1;
        } else {
          add = 0;
        }
        if (val.at(row + 1).at(col) > val.at(row).at(col) + add) {
          val.at(row + 1).at(col) = val.at(row).at(col) + add;
          next.push_back(std::make_pair(row + 1, col));
        }
      }
      if ((col - 1) >= 0) {
        if (sit.at(row).at(col - 1) == true) {
          add = 1;
        } else {
          add = 0;
        }
        if (val.at(row).at(col - 1) > val.at(row).at(col) + add) {
          val.at(row).at(col - 1) = val.at(row).at(col) + add;
          next.push_back(std::make_pair(row, col - 1));
        }
      }
      if ((col + 1) < N) {
        if (sit.at(row).at(col + 1) == true) {
          add = 1;
        } else {
          add = 0;
        }
        if (val.at(row).at(col + 1) > val.at(row).at(col) + add) {
          val.at(row).at(col + 1) = val.at(row).at(col) + add;
          next.push_back(std::make_pair(row, col + 1));
        }
      }
    }
    int min = 100000;
    for (int i = 0; i < N; ++i) {
      if (min > val.at(i).at(0)) {
        min = val.at(i).at(0);
      }
      if (min > val.at(i).at(N - 1)) {
        min = val.at(i).at(N - 1);
      }
      if (min > val.at(0).at(i)) {
        min = val.at(0).at(i);
      }
      if (min > val.at(N - 1).at(i)) {
        min = val.at(N - 1).at(i);
      }
    }
    ans += min;
  }
  std::cout << ans << std::endl;
  return 0;
}
