use std::cmp::min;
fn main() {
    proconio::input! {
        n: usize,
        p: [proconio::marker::Usize1;n*n],
    }
    let mut d = vec![0;n*n];
    for i in 0..n { for j in 0..n {
        d[i*n+j] = min(min(i,n-1-i),min(j,n-1-j));
    }}
    let mut g = vec![1;n*n];
    let mut t = 0;
    for p in p {
        t += d[p];
        g[p]=0;
        f(p,n,&g,&mut d);
    }
    println!("{}",t);
}
fn f(i: usize, n: usize, g: &Vec<usize>, d: &mut Vec<usize>) {
    let c = d[i]+g[i];
    let mut u = Vec::new();
    if i>=n {u.push(i-n);}
    if i<n*(n-1) {u.push(i+n);}
    if i%n!=0 {u.push(i-1);}
    if i%n!=n-1 {u.push(i+1);}
    for u in u { if d[u]>c {
        d[u]=c;
        f(u,n,g,d);
    }}
}