#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
using Graph = vector<vector<long long>>;
using ll = long long;
long long n;
void changeL(vector<vector<long long>> grid, vector<vector<long long>> exis,
             long long ni, long long nj, long long m) {
  long long nexl = nj;
  while (nexl >= 0) {
    grid[ni][nexl] = min(grid[ni][nexl], m);
    if (exis[ni][nexl] == 0) {
      nexl--;
    } else {
      break;
    }
  }
}
void changeR(vector<vector<long long>> grid, vector<vector<long long>> exis,
             long long ni, long long nj, long long m) {
  long long nexr = nj;
  while (nexr < n) {
    grid[ni][nexr] = min(grid[ni][nexr], m);
    if (exis[ni][nexr] == 0) {
      nexr++;
    } else {
      break;
    }
  }
}
void changeU(vector<vector<long long>> grid, vector<vector<long long>> exis,
             long long ni, long long nj, long long m) {
  long long nexu = ni;
  while (nexu >= 0) {
    grid[nexu][nj] = min(grid[nexu][nj], m);
    if (exis[nexu][nj] == 0) {
      nexu--;
    } else {
      break;
    }
  }
}
void changeD(vector<vector<long long>> grid, vector<vector<long long>> exis,
             long long ni, long long nj, long long m) {
  long long nexd = ni;
  while (nexd < n) {
    grid[nexd][nj] = min(grid[nexd][nj], m);
    if (exis[nexd][nj] == 0) {
      nexd++;
    } else {
      break;
    }
  }
}
signed main() {
  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) {
      long long nexu = ni - 1;
      while (nexu >= 0) {
        grid[nexu][nj] = min(grid[nexu][nj], m);
        if (exis[nexu][nj] == 0) {
          nexu--;
        } else {
          break;
        }
      }
    }
    if (ni + 1 < n) {
      long long nexd = ni + 1;
      while (nexd < n) {
        grid[ni + 1][nj] = min(grid[ni + 1][nj], m);
        if (exis[nexd][nj] == 0) {
          nexd++;
        } else {
          break;
        }
      }
    }
    if (nj - 1 >= 0) {
      long long nexl = nj - 1;
      while (nexl >= 0) {
        grid[ni][nexl] = min(grid[ni][nexl], m);
        if (exis[ni][nexl] == 0) {
          nexl--;
        } else {
          break;
        }
      }
    }
    if (nj + 1 < n) {
      long long nexr = nj + 1;
      while (nexr < n) {
        grid[ni][nexr] = min(grid[ni][nexr], m);
        if (exis[ni][nexr] == 0) {
          nexr++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
