use proconio::input;

#[allow(unused_imports)]
use proconio::marker::{Bytes, Chars, Usize1};
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

#[derive(Clone, Debug, Default)]
struct Struct;

//#[allow(dead_code)]
fn f(n: usize, v: &Vec<(usize, usize)>) {
    let mut dist = vec![vec![0; n]; n];
    let mut is_empty = vec![vec![false; n]; n];

    for y in 0..n {
        for x in 0..n {
            dist[y][x] = min(min(x, y), min(n - x - 1, n - y - 1));
            is_empty[y][x] = dist[y][x] == 0;
        }
    }

    let mut q: VecDeque<(usize, usize, usize)> = VecDeque::new();
    let mut ans = 0;
    for p in v {
        let (x, y) = *p;
        is_empty[y][x] = true;
        let new_dist = min(
            min(dist[y - 1][x], dist[y + 1][x]),
            min(dist[y][x - 1], dist[y][x + 1]),
        );
        ans += new_dist;
        q.push_back((x, y, new_dist)); // (x,y)の外周からの距離を更新する。隣のマスの外周からの距離はnew_distだ。

        while let Some((x, y, new_dist)) = q.pop_front() {
            let d = new_dist + if is_empty[y][x] { 0 } else { 1 };

            if dist[y][x] > d {
                dist[y][x] = d;
                q.push_back((y + 1, x, d));
                q.push_back((y - 1, x, d));
                q.push_back((y, x + 1, d));
                q.push_back((y, x - 1, d));
            }
        }
    }

    println!("{}", ans);
}

//#[proconio::fastout]
fn main() {
    input! {
        n:usize,
        p:[usize;n*n],
    }

    // 外周に1マス分の空きスペースを増やす
    let n2 = n + 2;
    let mut v: Vec<(usize, usize)> = Vec::with_capacity(n * n);

    for i in 0..n * n {
        let pp = p[i] - 1;
        let x = pp % n;
        let y = pp / n;
        v.push((x + 1, y + 1));
    }

    f(n2, &v);
}
