/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class BJoker {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      vector<vector<int>> dp(n, vector<int>(n, INT_MAX/2));
      deque<P> que;
      rep(i, n) {
        rep(j, n) {
          if (i==0 || i==n-1 || j==0 || j==n-1) {
            dp[i][j] = 0;
            que.emplace_back(0, i*n+j);
          }
        }
      }

      vector<vector<bool>> mat(n, vector<bool>(n, true));

      while(!que.empty()) {
        auto pa = que.front(); que.pop_front();
        auto c = pa.first;
        auto x = pa.second/n;
        auto y = pa.second%n;
        if (mat[x][y]) c++;
        if (x > 0 && dp[x-1][y] > c) {
          dp[x-1][y] = c;
          que.emplace_back(c, (x-1)*n+y);
        }
        if (x < n-1 && dp[x+1][y] > c) {
          dp[x+1][y] = c;
          que.emplace_back(c, (x+1)*n+y);
        }
        if (y > 0 && dp[x][y-1] > c) {
          dp[x][y-1] = c;
          que.emplace_back(c, x*n+y-1);
        }
        if (y < n-1 && dp[x][y+1] > c) {
          dp[x][y+1] = c;
          que.emplace_back(c, x*n+y+1);
        }
      }

      ll ret = 0;
      rep(i, n*n) {
        int p; cin >> p;
        p--;
        int x1 = p/n;
        int y1 = p%n;
        ret += dp[x1][y1];
        mat[x1][y1] = false;
        que.emplace_back(dp[x1][y1], x1*n+y1);

        while(!que.empty()) {
          auto pa = que.front(); que.pop_front();
          auto c = pa.first;
          auto x = pa.second/n;
          auto y = pa.second%n;
          if (mat[x][y]) c++;
          if (x > 0 && dp[x-1][y] > c) {
            dp[x-1][y] = c;
            que.emplace_back(c, (x-1)*n+y);
          }
          if (x < n-1 && dp[x+1][y] > c) {
            dp[x+1][y] = c;
            que.emplace_back(c, (x+1)*n+y);
          }
          if (y > 0 && dp[x][y-1] > c) {
            dp[x][y-1] = c;
            que.emplace_back(c, x*n+y-1);
          }
          if (y < n-1 && dp[x][y+1] > c) {
            dp[x][y+1] = c;
            que.emplace_back(c, x*n+y+1);
          }
        }

      }
      cout << ret << endl;


    }
};

signed main() {
  BJoker solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 4692