#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const double eps = 1e-9;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   inline bool checkBit(long long n, int i) { return n & (1LL << i); }   inline long long setBit(long long n, int i) { return n | (1LL << i); }   inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline bool isLeapYear(long long year) {     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);   }   inline void normal(long long &a) {     a %= 1000000007;     (a < 0) && (a += 1000000007);   }   inline long long modMul(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a * b) % 1000000007;   }   inline long long modAdd(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a + b) % 1000000007;   }   inline long long modSub(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     a -= b;     normal(a);     return a;   }   inline long long modPow(long long b, long long p) {     long long r = 1;     while (p) {       if (p & 1) r = modMul(r, b);       b = modMul(b, b);       p >>= 1;     }     return r;   }   inline long long modInverse(long long a) {     normal(a);     return modPow(a, 1000000007 - 2);   }   inline long long modDiv(long long a, long long b) {     return modMul(a, modInverse(b));   }   int dx[] = {0, 0, +1, -1};   int dy[] = {+1, -1, 0, 0};   int n, a[15][15], num[15][15], cur[105], dp[2][5][5][105], lagbe[2][5][105];   vector<int> vt[5][105];   int knightx[] = {-2, -2, +2, +2, -1, -1, +1, +1};   int knighty[] = {-1, +1, -1, +1, +2, -2, +2, -2};   void valid(int ro, int col) {     for (int i = 0; i < 8; i++) {       int tmp = ro + knightx[i];       int tpp = col + knighty[i];       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[1][num[ro][col]].push_back(num[tmp][tpp]);     }     for (int i = 1; i <= 10; i++) {       int tmp = ro + i;       int tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro + i;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);     }     for (int i = 1; i <= 10; i++) {       int tmp = ro + i;       int tpp = col;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro;       tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);     }   }   bool one(int beg, int fin, int ro, int col, int &mov, int &change) {     if (ro == col) {       change++;       return true;     }     if (beg != fin) return false;     for (int i = 0; i < vt[beg][ro].size(); i++)       if (vt[beg][ro][i] == col) {         mov++;         return true;       }     return false;   }   bool berkoro(int num, int beg, int fin, int ro, int col, int &mov,                int &change) {     int res = 0, val1 = 0, val2 = 0;     if (num == 1) return one(beg, fin, ro, col, mov, change);     for (int i = 0, tmp; !res && i < vt[beg][ro].size(); i++) {       tmp = vt[beg][ro][i];       val1 = 1, val2 = 0;       res |= berkoro(num - 1, beg, fin, tmp, col, val1, val2);     }     for (int i = 1; !res && i <= 3; i++) {       if (beg == i) continue;       val2 = 1, val1 = 0;       res |= berkoro(num - 1, i, fin, ro, col, val1, val2);     }     if (res) mov += val1, change += val2;     return res;   }   void process(int beg, int fin, int ro, int col, int &mov, int &change) {     for (int i = 1; i <= 4; i++) {       mov = 0, change = 0;       if (berkoro(i, beg, fin, ro, col, mov, change)) return;     }   }   void process(int mark, int pos, int &mov, int &change) {     int &ret1 = lagbe[0][mark][pos];     int &ret2 = lagbe[1][mark][pos];     if (ret1 != -1) {       mov += ret1;       change += ret2;       return;     }     if (pos == n * n) return;     int val1 = 0, val2 = 0, res1 = 1e9, res2 = 1e9;     if (pos == 1) {       for (int i = 1; i <= 3; i++)         for (int j = 1; j <= 3; j++) {           val1 = dp[0][i][j][pos], val2 = dp[1][i][j][pos];           process(j, pos + 1, val1, val2);           if (val1 + val2 < res1 + res2)             res1 = val1, res2 = val2;           else if (val1 + val2 == res1 + res2 && val2 < res2)             res1 = val1, res2 = val2;         }     } else {       for (int i = 1; i <= 3; i++) {         val1 = dp[0][mark][i][pos], val2 = dp[1][mark][i][pos];         process(i, pos + 1, val1, val2);         if (val1 + val2 < res1 + res2)           res1 = val1, res2 = val2;         else if (val1 + val2 == res1 + res2 && val2 < res2)           res1 = val1, res2 = val2;       }     }     mov += res1, change += res2;     ret1 = res1, ret2 = res2;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1, cnt = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         cin >> a[i][j];         num[i][j] = cnt;         cur[a[i][j]] = cnt;         cnt++;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) valid(i, j);     for (int i = 1; i < n * n; i++) {       for (int k = 1; k <= 3; k++)         for (int l = 1; l <= 3; l++)           process(k, l, cur[i], cur[i + 1], dp[0][k][l][i], dp[1][k][l][i]);     }     memset(lagbe, -1, sizeof(lagbe));     int mov = 0, change = 0;     process(0, 1, mov, change);     cout << mov + change <<     << change <<   n ;     return 0;   }