#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
vector<vector<int>> t(4 * 100001, vector<int>(26, 0));
long long binary_exp(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % MOD;
    }
    a = (a * a) % MOD;
    b = b >> 1;
  }
  return ans;
}
void buildseg(long long x, long long l, long long r, string &a) {
  if (l == r) {
    t[x][a[l] - 'a']++;
  } else {
    long long mid = (l + r) / 2;
    buildseg(2 * x + 1, l, mid, a);
    buildseg(2 * x + 2, mid + 1, r, a);
    vector<int> v(26, 0);
    for (long long i = 0; i < 26; i++) {
      if (t[2 * x + 1][i] > 0) {
        v[i] += t[2 * x + 1][i];
      }
      if (t[2 * x + 2][i] > 0) {
        v[i] += t[2 * x + 2][i];
      }
    }
    t[x] = v;
  }
}
vector<int> sum(long long x, long long l1, long long r1, long long l,
                long long r) {
  if (l > r) {
    vector<int> v(26, 0);
    return v;
  } else if (l1 == l && r1 == r) {
    return t[x];
  } else {
    long long mid = (l1 + r1) / 2;
    vector<int> b;
    b = sum(2 * x + 1, l1, mid, l, min(r, mid));
    vector<int> d;
    d = sum(2 * x + 2, mid + 1, r1, max(l, mid + 1), r);
    vector<int> v(26, 0);
    for (long long i = 0; i < 26; i++) {
      if (b[i] > 0) {
        v[i] += b[i];
      }
      if (d[i] > 0) {
        v[i] += d[i];
      }
    }
    return v;
  }
}
void update(long long x, long long l, long long r, char value, long long pos,
            string &a) {
  if (l == r) {
    t[x][a[pos] - 'a'] = max(0, t[x][a[pos] - 'a'] - 1);
    t[x][value - 'a']++;
    a[pos] = value;
  } else {
    long long mid = (l + r) / 2;
    if (mid >= pos) {
      update(2 * x + 1, l, mid, value, pos, a);
    } else {
      update(2 * x + 2, mid + 1, r, value, pos, a);
    }
    vector<int> v(26, 0);
    for (long long i = 0; i < 26; i++) {
      if (t[2 * x + 1][i] > 0) {
        v[i] += t[2 * x + 1][i];
      }
      if (t[2 * x + 2][i] > 0) {
        v[i] += t[2 * x + 2][i];
      }
    }
    t[x] = v;
  }
}
void printseg(string a, long long n) {
  for (long long i = 0; i < 4 * n; i++) {
    for (long long j = 0; j < 26; j++) {
      cout << t[i][j] << " ";
    }
    cout << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T;
  T = 1;
  while (T--) {
    long long n;
    cin >> n;
    vector<int> a(n * n);
    for (int i = 0; i < n * n; i++) {
      cin >> a[i];
    }
    vector<int> vis(n * n + 1, 0);
    int ans1 = 0;
    for (int j = 0; j < n * n; j++) {
      int i = a[j];
      vis[a[j]] = 1;
      if ((i / n) == 0 || (i == n)) {
        continue;
      }
      if (i / n == (n - 1) || i == n * n) {
        continue;
      }
      int k = i / n;
      int l = i % n - 1;
      int r = n - i / n - 1;
      int q = (n - (i % n)) % n;
      if (l == 0 || q == 0) {
        continue;
      }
      int c = 0;
      int m = i;
      while (k--) {
        if (vis[m - n] == 0) {
          c++;
        }
        m = m - n;
      }
      int ans = c;
      c = 0;
      m = i;
      while (l--) {
        if (vis[m - 1] == 0) {
          c++;
        }
        m--;
      }
      ans = min(ans, c);
      c = 0;
      m = i;
      while (r--) {
        if (vis[m + n] == 0) {
          c++;
        }
        m = m + n;
      }
      ans = min(ans, c);
      c = 0;
      m = i;
      while (q--) {
        if (vis[m + 1] == 0) {
          c++;
        }
        m++;
      }
      ans = min(ans, c);
      ans1 += ans;
    }
    cout << ans1 << endl;
  }
  return 0;
}
