#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}
const int MAXN = 503;
int n;
int f[MAXN];
int val[MAXN];
int toCell(int i, int j) { return (i - 1) * n + j; }
int FIND(int w) {
  if (w == f[w]) {
    return w;
  }
  f[w] = FIND(f[w]);
  return f[w];
}
bool vis[MAXN];
void UNION(int a, int b) {
  if (a == b) {
    return;
  }
  int fa = FIND(a);
  int fb = FIND(b);
  if (val[fa] == val[fb]) {
    return;
  }
  if (val[fa] < val[fb]) {
    return;
  }
  if (val[fa] < val[fb]) {
    f[fb] = fa;
  } else {
    f[fa] = fb;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      val[toCell(i, j)] = min(min(i - 1, j - 1), min(n - i, n - j));
      f[toCell(i, j)] = toCell(i, j);
    }
  }
  LL res = 0;
  vector<int> dirs = {-1, 1, n, -n};
  for (int i = 1; i <= n * n; ++i) {
    int x;
    cin >> x;
    int fx = FIND(x);
    int toAdd = val[x];
    for (auto dir : dirs) {
      int neww = x + dir;
      if (neww > n * n || neww < 1) {
        continue;
      }
      if (x % n == 0 && dir == 1) {
        continue;
      }
      if ((x - 1) % n == 0 && dir == -1) {
        continue;
      }
      toAdd = min(toAdd, val[FIND(neww)] + (vis[neww] == false ? 1 : 0));
    }
    int neww = x + 1;
    for (int j = 1; j <= n; ++j) {
      if (neww % n == 0) {
        break;
      }
      if (vis[neww] == false) {
        toAdd = min(toAdd, val[FIND(neww)] + 1);
        break;
      }
      neww++;
    }
    neww = x - 1;
    for (int j = 1; j <= n; ++j) {
      if ((neww - 1) % n == 0) {
        break;
      }
      if (vis[neww] == false) {
        toAdd = min(toAdd, val[FIND(neww)] + 1);
        break;
      }
      neww--;
    }
    neww = x + n;
    for (int j = 1; j <= n; ++j) {
      if (neww > n * n) {
        break;
      }
      if (vis[neww] == false) {
        toAdd = min(toAdd, val[FIND(neww)] + 1);
        break;
      }
      neww += n;
    }
    neww = x - n;
    for (int j = 1; j <= n; ++j) {
      if (neww < 0) {
        break;
      }
      if (vis[neww] == false) {
        toAdd = min(toAdd, val[FIND(neww)] + 1);
        break;
      }
      neww -= n;
    }
    (x, FIND(x), val[FIND(x)], FIND(14), toAdd);
    val[fx] = max(val[fx], 0);
    vis[x] = true;
    for (auto dir : dirs) {
      int neww = x + dir;
      if (neww > n * n || neww < 1) {
        continue;
      }
      if (x % n == 0 && dir == 1) {
        continue;
      }
      if ((x - 1) % n == 0 && dir == -1) {
        continue;
      }
      int fn = FIND(neww);
      int fx = FIND(x);
      UNION(neww, x);
    }
    if (x == 13) {
      (val[FIND(14)], FIND(14));
    }
    res += toAdd;
  }
  cout << res << endl;
}
