#include <bits/stdc++.h>
constexpr long long MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846;
const long long INF = 1e12;
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
const long long ddx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const long long ddy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const long long month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const long long umonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long intpow(long long a, long long n) {
  if (n == 0)
    return 1;
  else {
    for (long long i = 0; i < n - 1; i++) a *= a;
    return a;
  }
}
long long MODPOW(long long a, long long n, long long mod) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) ans = ans * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ans;
}
long long FACT(long long a) {
  if (a == 0)
    return 1;
  else
    return a * FACT(a - 1);
}
long long MODFACT(long long a, long long mod) {
  long long ans = 1;
  for (long long i = 1; i <= a; i++) {
    ans *= i;
    ans %= MOD;
  }
  return ans;
}
long long nPr(long long n, long long r) {
  long long s = n - r + 1;
  long long sum = 1;
  for (long long i = s; i <= n; i++) sum *= i;
  return sum;
}
long long MODnPr(long long n, long long r, long long mod) {
  long long s = n - r + 1;
  long long sum = 1;
  for (long long i = s; i <= n; i++) {
    sum *= i;
    sum = sum % MOD;
  }
  return sum;
}
long long nCr2(long long n, long long r) {
  return FACT(n) / (FACT(r) * FACT(n - r));
}
long long GCD(long long a, long long b) {
  if (a < b) (a += b, b = a - b, a -= b);
  if (b == 0) return a;
  if (a % b == 0) return b;
  return GCD(b, a % b);
}
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
long long NUMOFDIV(long long n) {
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (n % i == 0) ans++;
  }
  return ans;
}
long long CEIL1(long long n) { return (n + 9) / 10 * 10; }
long long GetDigit(long long n) { return log10(n) + 1; }
long long DIGIT(long long n, long long k) {
  for (long long i = 0; i < k - 1; i++) n /= 10;
  return n % 10;
}
long long DIGITSUM(long long n) {
  long long sum = 0, dig;
  while (n) {
    dig = n % 10;
    sum += dig;
    n /= 10;
  }
  return sum;
}
long long DIVTIME(long long n, long long k) {
  long long div = 0;
  while (n % k == 0) {
    div++;
    n /= k;
  }
  return div;
}
long long binary(long long n) {
  long long ans = 0;
  for (long long i = 0; n > 0; i++) {
    ans += n % 2 * intpow(10, i);
    n /= 2;
  }
  return ans;
}
long long intabs(long long n) {
  if (n < 0)
    return -1 * n;
  else
    return n;
}
long double LOG(long long a, long long b) { return log(b) / log(a); }
long double DISTANCE(long long x1, long long y1, long long x2, long long y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
long double WatchAngle(long long h, long long m) {
  h %= 12;
  long double mm = 6.0 * m;
  long double nn = 30.0 * h + 0.5 * m;
  return std::min(fabs(mm - nn), 360.0 - fabs(nn - mm));
}
inline bool BETWEEN(long long x, long long min, long long max) {
  if (min <= x && x <= max)
    return true;
  else
    return false;
}
inline bool between(long long x, long long min, long long max) {
  if (min < x && x < max)
    return true;
  else
    return false;
}
inline bool isPRIME(long long x) {
  if (x == 1) return false;
  if (x == 2) return true;
  if (x % 2 == 0) return false;
  long double sqrtx = sqrt(x);
  for (long long i = 3; i <= sqrtx; i += 2) {
    if (x % i == 0) return false;
  }
  return true;
}
inline bool isSQRT(long long n) {
  if (sqrt(n) == (long long)sqrt(n))
    return true;
  else
    return false;
}
inline bool isURU(long long y) {
  if (y % 400 == 0) return 1;
  if (y % 100 == 0) return 0;
  if (y % 4 == 0) return 1;
  return 0;
}
using namespace std;
using Graph = vector<vector<long long>>;
signed main() {
  long long N;
  cin >> N;
  vector<long long> P(N * N);
  vector<vector<bool>> p1(N, vector<bool>(N, 1));
  for (long long i = 0; i < N * N; i++) {
    cin >> P[i];
    P[i]--;
  }
  long long ans = 0;
  for (long long i = 0; i < N * N; i++) {
    long long x = 100;
    long long sx = P[i] % N;
    long long sy = P[i] / N;
    vector<vector<long long>> dist(N, vector<long long>(N, -1));
    dist[sx][sy] = 0;
    queue<pair<long long, long long>> que;
    que.push({sx, sy});
    while (!que.empty()) {
      pair<long long, long long> cu = que.front();
      que.pop();
      for (long long j = 0; j < 4; j++) {
        long long nx = cu.first + dx[j];
        long long ny = cu.second + dy[j];
        if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
        if (dist[nx][ny] == -1) {
          que.push({nx, ny});
          if (p1[nx][ny])
            dist[nx][ny] = dist[cu.first][cu.second] + 1;
          else
            dist[nx][ny] = dist[cu.first][cu.second];
        }
      }
    }
    for (long long j = 0; j < N; j++) x = min({x, dist[0][j]});
    for (long long j = 0; j < N; j++) x = min(x, dist[j][0]);
    for (long long j = 0; j < N; j++) x = min(x, dist[N - 1][j]);
    for (long long j = 0; j < N; j++) x = min(x, dist[j][N - 1]);
    ans += x;
    p1[sx][sy] = false;
  }
  cout << ans;
}
