#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
using Graph = vector<vector<long long>>;
using ll = long long;
ll gcd(ll A, ll B) {
  if (B == 0) return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
bool root(string s) {
  long long n = s.length();
  if (n == 2) {
    if (s[0] == s[1]) {
      return true;
    } else
      return false;
  }
  if (n == 3) {
    if (s[0] == s[2]) {
      return true;
    } else
      return false;
  }
  return false;
}
bool IsPrime(long long num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (long long i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
vector<long long> seen;
vector<ll> h;
void dfs(const Graph &G, long long v) {
  seen[v] = 0;
  P good = make_pair(v, h[v]);
  for (auto next_v : G[v]) {
    if (seen[next_v] != -1) continue;
    if (h[next_v] > good.second) {
      good = make_pair(next_v, h[next_v]);
    }
    dfs(G, next_v);
  }
  if (!(h[v] == good.second && v == good.first)) {
    seen[good.first] = 1;
  }
}
long long n;
void dfs(string s, char mx) {
  if (s.length() == n) {
    cout << s << endl;
  } else {
    for (char c = 'a'; c <= mx; c++) {
      dfs(s + c, ((c == mx) ? (char)(mx + 1) : mx));
    }
  }
}
vector<long long> dsy(vector<long long> dis,
                      vector<vector<pair<long long, long long>>> g,
                      long long start, long long goal) {
  long long n = dis.size();
  priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                 greater<pair<int64_t, int64_t>>>
      Q;
  dis[start] = 0;
  Q.push(make_pair(0, start));
  while (!Q.empty()) {
    long long pos = Q.top().second;
    Q.pop();
    for (long long i = 0; i < g.at(pos).size(); i++) {
      long long to = g.at(pos).at(i).first;
      long long cost = g.at(pos).at(i).second;
      if (dis[to] == -1 || dis[to] > dis[pos] + cost) {
        dis[to] = dis[pos] + cost;
        Q.push(make_pair(dis[to], to));
      }
    }
  }
  return dis;
}
const long double PI = 3.14159265358979323846;
signed main() {
  long long n;
  cin >> n;
  long long n2 = pow(n, 2);
  long long ex[n2];
  for (long long i = 0; i < n2; i++) {
    long long pi;
    cin >> pi;
    ex[i] = pi - 1;
  }
  vector<vector<long long>> grid(n, vector<long long>(n, 0));
  vector<vector<long long>> exis(n, vector<long long>(n, 1));
  for (long long i = 0; i < n; i++) {
    if (i == 0) continue;
    if (i == n - 1) continue;
    for (long long j = 0; j < n; j++) {
      if (j == 0 || j == n - 1) continue;
      grid[i][j] = min(min(i, n - (i + 1)), min(j, n - (j + 1)));
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n2; i++) {
    long long exp = ex[i];
    long long ni = exp / n;
    long long nj = exp - ni * n;
    exis[ni][nj] = 0;
    long long m = grid[ni][nj];
    if (ni - 1 >= 0) {
      m = min(m, grid[ni - 1][nj] + exis[ni - 1][nj]);
    }
    if (ni + 1 < n) {
      m = min(m, grid[ni + 1][nj] + exis[ni + 1][nj]);
    }
    if (nj - 1 >= 0) {
      m = min(m, grid[ni][nj - 1] + exis[ni][nj - 1]);
    }
    if (nj + 1 < n) {
      m = min(m, grid[ni][nj + 1] + exis[ni][nj + 1]);
    }
    ans += m;
    if (ni - 1 >= 0) {
      if (grid[ni - 1][nj] != 0) {
        grid[ni - 1][nj] = min(grid[ni - 1][nj], m);
      }
    }
    if (ni + 1 < n) {
      if (grid[ni + 1][nj] != 0) {
        grid[ni + 1][nj] = min(grid[ni + 1][nj], m);
      }
    }
    if (nj - 1 >= 0) {
      if (grid[ni][nj - 1] != 0) {
        grid[ni][nj - 1] = min(grid[ni][nj - 1], m);
      }
    }
    if (nj + 1 < n) {
      if (grid[ni][nj + 1] != 0) {
        grid[ni][nj + 1] = min(grid[ni][nj + 1], m);
      }
    }
  }
  cout << ans << endl;
}
