#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

    int n;
    cin >> n;
    int n2 = n*n;
    vector<int> p(n2);
    FOR(i, n2) cin >> p[i];
    vector<vector<int>> A(n+1, vector<int>(n+1));
    int x = 1;
    FOR1(i, n) FOR1(j, n) A[i][j] = x++;

    set<pii> S;
    FOR(i, n2){
        int key = p[i];
        int row = (key-1)/n + 1;
        int col = (key-1)%n + 1;

        A[row][col] = 0;
        int left = 0, right = 0;
        FOR1(j, n) if (A[row][j]) if (j < col) left++; else right++;
        int up = 0, down = 0;
        FOR1(i, n) if (A[i][col]) if (i < row) up++; else down++;

        int nb = min( min(left, right), min(up, down));
        if (nb == left){
            FOR1(j, col-1) if (A[row][j]) S.insert({A[row][j], key});
        }
        else if (nb == right){
            for(int j = col+1; j<=n; j++) if (A[row][j]) S.insert({A[row][j], key});
        }
        else if (nb == up){
            FOR1(i, row-1) if (A[i][col]) S.insert({A[i][col], key});
        }
        else if (nb == down){
            for(int i = row+1; i<=n; i++) if (A[i][col]) S.insert({key, A[i][col]});
        }
    }
    cout << S.size() << endl;

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}