#include <bits/stdc++.h>
using namespace std;
const long long INF = 2000000000;
const long long INF2 = 8000000000000000000;
const long long large_P = 1000000007;
long long my_pow(long long N, long long a, long long M) {
  long long tempo;
  if (a == 0) {
    return 1;
  } else {
    if (a % 2 == 0) {
      tempo = my_pow(N, a / 2, M);
      return (tempo * tempo) % M;
    } else {
      tempo = my_pow(N, a - 1, M);
      return (tempo * N) % M;
    }
  }
}
long long my_combination(long long N, long long a, long long M) {
  long long answer = 1;
  for (long long i = 0; i < (a); i++) {
    answer *= (N - i);
    answer %= M;
  }
  for (long long i = 0; i < (a); i++) {
    answer *= my_pow(i + 1, M - 2, M);
    answer %= M;
  }
  return answer;
}
long long my_gcd(long long& a, long long& b) {
  if (b == 0) return a;
  long long temp = a % b;
  return my_gcd(b, temp);
}
void ll_to_vector(long long base, long long n, vector<long long>& v) {
  long long tempo = n;
  long long tempo2 = n;
  long long n_digit = 1;
  while (tempo2 >= 10) {
    tempo2 /= 10;
    n_digit++;
  }
  v.assign(n_digit, 0);
  for (long long i = 0; i < n_digit; i++) {
    v.at(i) = tempo / pow(base, n_digit - 1 - i);
    tempo -= v.at(i) * pow(base, n_digit - 1 - i);
  }
}
long long char_to_int(char c) {
  switch (c) {
    case '0':
      return 0;
    case '1':
      return 1;
    case '2':
      return 2;
    case '3':
      return 3;
    case '4':
      return 4;
    case '5':
      return 5;
    case '6':
      return 6;
    case '7':
      return 7;
    case '8':
      return 8;
    case '9':
      return 9;
    default:
      return 0;
  }
}
void prime_judge(vector<bool>& prime_or_not) {
  prime_or_not.at(0) = false;
  prime_or_not.at(1) = false;
  long long n = prime_or_not.size() - 1;
  long long region = (long long)pow((long double)(n), 0.50) + 1;
  for (long long i = 2; i <= n / 2; i++) {
    prime_or_not.at(2 * i) = false;
  }
  for (long long i2 = 1; i2 < n / 2; i2++) {
    long long i = 2 * i2 + 1;
    if (i < region && prime_or_not.at(i)) {
      long long j = i * i;
      while (j < n + 1) {
        prime_or_not.at(j) = false;
        j += 2 * i;
      }
    }
  }
};
map<long long, long long> divide_to_prime(long long target) {
  map<long long, long long> res;
  long long upper_lim = ceil(pow((long double)(target), 0.50));
  vector<bool> prime_or_not(upper_lim + 1, true);
  if (upper_lim < 20) prime_or_not.assign(25, true);
  prime_judge(prime_or_not);
  long long tempo = target;
  for (long long i = 0; i < ((upper_lim + 1)); i++) {
    if (prime_or_not.at(i)) {
      while (tempo % i == 0) {
        tempo /= i;
        res[i]++;
      }
    }
  }
  if (tempo != 1) res[tempo]++;
  return res;
}
const vector<pair<long long, long long>> delta = {
    make_pair(-1, 0), make_pair(1, 0), make_pair(0, -1), make_pair(0, 1)};
long long BFS_labyrinth(deque<pair<long long, long long>>& que,
                        vector<vector<long long>>& dist,
                        const vector<vector<long long>> Remain,
                        vector<vector<long long>> cost0, long long imin,
                        long long imax, long long jmin, long long jmax) {
  long long n = dist.size();
  long long m = dist.at(0).size();
  while (!que.empty()) {
    long long temp_i, temp_j;
    pair<long long, long long> tempo = que.front();
    que.pop_front();
    temp_i = tempo.first;
    temp_j = tempo.second;
    if (temp_i == imin || temp_i == imax || temp_j == jmin || temp_j == jmax ||
        cost0.at(temp_i).at(temp_j) == 0) {
      return dist.at(temp_i).at(temp_j);
    }
    for (long long k = 0; k < (4); k++) {
      long long i = delta.at(k).first;
      long long j = delta.at(k).second;
      if (temp_i + i < imin || imax + 1 <= temp_i + i) continue;
      if (temp_j + j < jmin || jmax + 1 <= temp_j + j) continue;
      if (dist.at(temp_i + i).at(temp_j + j) != -1) continue;
      dist.at(temp_i + i).at(temp_j + j) =
          dist.at(temp_i).at(temp_j) + Remain.at(temp_i + i).at(temp_j + j);
      if (Remain.at(temp_i + i).at(temp_j + j) == 0) {
        que.push_front(make_pair(temp_i + i, temp_j + j));
      } else {
        que.push_back(make_pair(temp_i + i, temp_j + j));
      }
    }
  }
  return -1;
}
signed main() {
  long long N;
  cin >> N;
  vector<long long> P(N * N);
  for (long long i = 0; i < (N * N); i++) {
    scanf("%lld", &P.at(i));
  }
  vector<vector<long long>> Remain(N, vector<long long>(N, 1));
  vector<vector<long long>> cost0(N, vector<long long>(N, 1));
  long long res = 0;
  long long imin = 0, imax = N - 1, jmin = 0, jmax = N - 1;
  for (long long i = 0; i < (N); i++) {
    cost0.at(i).at(0) = 0;
    cost0.at(i).at(N - 1) = 0;
    cost0.at(0).at(i) = 0;
    cost0.at(N - 1).at(i) = 0;
  }
  for (long long i = 0; i < (N * N); i++) {
    long long h, w;
    h = (P.at(i) - 1) / N;
    w = (P.at(i) - 1) % N;
    deque<pair<long long, long long>> que;
    que.push_front(make_pair(h, w));
    vector<vector<long long>> dist(N, vector<long long>(N, -1));
    dist.at(h).at(w) = 0;
    long long ret =
        BFS_labyrinth(que, dist, Remain, cost0, imin, imax, jmin, jmax);
    res += ret;
    if (ret == 0) {
      cost0.at(h).at(w) = 0;
      for (long long k = 0; k < (4); k++) {
        long long dh = delta.at(k).first;
        long long dw = delta.at(k).second;
        if (h + dh < 0 || N <= h + dh) continue;
        if (w + dw < 0 || N <= w + dw) continue;
        cost0.at(h + dh).at(w + dw) = 0;
      }
    }
    Remain.at(h).at(w) = 0;
    bool ok = true;
    for (long long j = 0; j < (N); j++) {
      if (Remain.at(imin).at(j) == 1) {
        ok = false;
        break;
      }
    }
    if (ok) imin++;
    ok = true;
    for (long long j = 0; j < (N); j++) {
      if (Remain.at(imax).at(j) == 1) {
        ok = false;
        break;
      }
    }
    if (ok) imax--;
    ok = true;
    for (long long i = 0; i < (N); i++) {
      if (Remain.at(i).at(jmin) == 1) {
        ok = false;
        break;
      }
    }
    if (ok) jmin++;
    ok = true;
    for (long long i = 0; i < (N); i++) {
      if (Remain.at(i).at(jmax) == 1) {
        ok = false;
        break;
      }
    }
    if (ok) jmax--;
  }
  std::cout << res << endl;
}
