#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string ret = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      ret += ", ";
    }
    first = false;
    ret += to_string(v[i]);
  }
  ret += "}";
  return ret;
}
template <size_t N>
string to_string(bitset<N> v) {
  string ret = "";
  for (size_t i = 0; i < N; i++) {
    ret += static_cast<char>('0' + v[i]);
  }
  return ret;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string ret = "{";
  for (const auto &x : v) {
    string s = to_string(x);
    if (!first) {
      ret += ", ";
    }
    if (s[0] == '{') {
      s = "\n" + s;
    }
    first = false;
    ret += s;
  }
  if (ret[ret.size() - 1] == '}') {
    ret += "\n}";
  } else {
    ret += "}";
  }
  return ret;
}
template <typename A, typename... Lest>
string to_string(A *v, int n, Lest... L) {
  bool first = true;
  string ret = "{";
  for (int i = 0; i < n; i++) {
    auto x = v[i];
    string s = to_string(x, L...);
    if (!first) {
      ret += ", ";
    }
    if (s[0] == '{') {
      s = "\n" + s;
    }
    first = false;
    ret += s;
  }
  if (ret[ret.size() - 1] == '}') {
    ret += "\n}";
  } else {
    ret += "}";
  }
  return ret;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void name_disp(string s) {
  cerr << "-------" << endl;
  cerr << s << ": " << endl;
}
void val_disp() { cerr << endl; }
template <typename First, typename... Lest>
void val_disp(First F, Lest... L) {
  cerr << to_string(F) << endl;
  val_disp(L...);
}
template <typename A, typename... Lest>
void val_disp(A *v, Lest... L) {
  cerr << to_string(v, L...) << endl;
  val_disp();
}
const int INF = 1002003004;
const int M = 100000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
bool field[550][550];
int cost[550][550];
signed main() {
  int n, p[252525];
  scanf("%d", &n);
  int n2 = n * n;
  for (int i = 0; i < n2; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      field[i][j] = true;
    }
  int ret = 0;
  for (int hito = 0; hito < n2; hito++) {
    int sh = (p[hito] - 1) / n;
    int sw = (p[hito] - 1) % n;
    field[sh][sw] = false;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cost[i][j] = INF;
      }
    queue<pair<int, int> > que;
    que.push(make_pair(sh, sw));
    cost[sh][sw] = 0;
    while (!que.empty()) {
      int h = que.front().first;
      int w = que.front().second;
      que.pop();
      for (int i = 0; i < 4; i++) {
        int nh = h + dir_4[i][0];
        int nw = w + dir_4[i][1];
        if (nh < 0 || n <= nh || nw < 0 || n <= nw) continue;
        int nc;
        if (field[nh][nw]) {
          nc = 1;
        } else {
          nc = 0;
        }
        if (cost[nh][nw] > cost[h][w] + nc) {
          cost[nh][nw] = cost[h][w] + nc;
          que.push(make_pair(nh, nw));
        }
      }
    }
    int rett = INF;
    for (int i = 0; i < n; i++) {
      rett = min(rett, cost[0][i]);
      rett = min(rett, cost[n - 1][i]);
      rett = min(rett, cost[i][0]);
      rett = min(rett, cost[i][n - 1]);
    }
    ret += rett;
  }
  printf("%d\n", ret);
  return 0;
};
