#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b, len;
  edge(int a = 0, int b = 0, int len = 0) {
    this->a = a;
    this->b = b;
    this->len = len;
  }
};
int n;
vector<vector<vector<edge>>> g;
vector<vector<int>> used;
vector<vector<int>> d;
void bfs(int i, int j) {
  used.assign(n, vector<int>(n, 0));
  d.assign(n, vector<int>(n, 1e9));
  deque<pair<int, int>> q;
  q.push_back({i, j});
  used[i][j] = 1;
  d[i][j] = 0;
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop_front();
    int i = now.first;
    int j = now.second;
    for (edge &to : g[i][j]) {
      int new_i = to.a;
      int new_j = to.b;
      int len = to.len;
      if (!used[new_i][new_j]) {
        used[new_i][new_j] = 1;
        d[new_i][new_j] = d[i][j] + len;
        if (len == 0) {
          q.push_front({new_i, new_j});
        } else {
          q.push_back({new_i, new_j});
        }
      }
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  g.resize(n, vector<vector<edge>>(n));
  vector<int> need(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> need[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i + 1 < n) g[i][j].push_back(edge(i + 1, j, 1));
      if (i - 1 >= 0) g[i][j].push_back(edge(i - 1, j, 1));
      if (j + 1 < n) g[i][j].push_back(edge(i, j + 1, 1));
      if (j - 1 >= 0) g[i][j].push_back(edge(i, j - 1, 1));
    }
  }
  int ans = 0;
  for (int &now : need) {
    int i = (now - 1) / n;
    int j = now - i * n - 1;
    bfs(i, j);
    int now_ans = 1e9;
    for (int i = 0; i < n; i++) {
      now_ans = min(now_ans, d[0][i]);
      now_ans = min(now_ans, d[n - 1][i]);
      now_ans = min(now_ans, d[i][0]);
      now_ans = min(now_ans, d[i][n - 1]);
    }
    ans += now_ans;
    if (i + 1 < n) {
      for (int k = 0; k < 4; k++) {
        if (g[i + 1][j][k].a == i && g[i + 1][j][k].b == j) {
          g[i + 1][j][k].len = 0;
        }
      }
    }
    if (i - 1 >= 0) {
      for (int k = 0; k < 4; k++) {
        if (g[i - 1][j][k].a == i && g[i - 1][j][k].b == j) {
          g[i - 1][j][k].len = 0;
        }
      }
    }
    if (j + 1 < n) {
      for (int k = 0; k < 4; k++) {
        if (g[i][j + 1][k].a == i && g[i][j + 1][k].b == j) {
          g[i][j + 1][k].len = 0;
        }
      }
    }
    if (j - 1 >= 0) {
      for (int k = 0; k < 4; k++) {
        if (g[i][j - 1][k].a == i && g[i][j - 1][k].b == j) {
          g[i][j - 1][k].len = 0;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
