macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
macro_rules !min {($e :expr ) =>{$e } ;($e :expr ,$($es :expr ) ,+) =>{std ::cmp ::min ($e ,min !($($es ) ,+) ) } ;}
pub struct GridGraph {
    height: usize,
    width: usize,
}
impl GridGraph {
    pub fn new(height: usize, width: usize) -> Self {
        Self { height, width }
    }
    pub fn len_adjacency(&self, x: usize, y: usize) -> usize {
        (x + 1 < self.height) as usize
            + (y + 1 < self.width) as usize
            + (x > 0) as usize
            + (y > 0) as usize
    }
    pub fn for_each_adjacency<F: FnMut(usize, usize)>(&self, x: usize, y: usize, mut f: F) {
        if x + 1 < self.height {
            f(x + 1, y);
        }
        if y + 1 < self.width {
            f(x, y + 1);
        }
        if x > 0 {
            f(x - 1, y);
        }
        if y > 0 {
            f(x, y - 1);
        }
    }
}
fn main() {
    input!(n, p: [usize1; n * n]);
    let mut c = vec![vec![0; n]; n];
    let mut rem = vec![vec![true; n]; n];
    for i in 0..n {
        for j in 0..n {
            c[i][j] = min!(i, n - i - 1, j, n - j - 1);
        }
    }
    let mut ans = 0;
    let g = GridGraph::new(n, n);
    for &k in p.iter() {
        let x = k / n;
        let y = k % n;
        ans += c[x][y];
        rem[x][y] = false;
        let mut st = vec![(x, y)];
        while let Some((x, y)) = st.pop() {
            let d = c[x][y] + rem[x][y] as usize;
            g.for_each_adjacency(x, y, |nx, ny| {
                if c[nx][ny] > d {
                    c[nx][ny] = d;
                    st.push((nx, ny));
                }
            });
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 9628