use std::cmp::min;
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: ty),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, p: [usize; n * n]);
    let mut board = vec![vec![0; n]; n];
    for i in 0..n {
        for j in 0..n {
            board[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
        }
    }
    let mut leaved = vec![vec![false; n]; n];
    let mut ret = 0;
    let dx = [1isize, 0, -1, 0];
    let dy = [0isize, 1, 0, -1];
    for i in 0..(n * n) {
        let x = (p[i] - 1) / n;
        let y = (p[i] - 1) % n;
        ret += board[x][y];

        let mut q = std::collections::VecDeque::new();
        let mut seen = vec![vec![false; n]; n];
        q.push_back((x, y));
        leaved[x][y] = true;
        seen[x][y] = true;

        while let Some((x, y)) = q.pop_front() {
            let s = board[x][y];
            let ns = if leaved[x][y] { s } else { s + 1 };
            for i in 0..4 {
                let nx = x as isize + dx[i];
                let ny = y as isize + dy[i];
                if nx < 0 || nx >= n as isize || ny < 0 || ny >= n as isize {
                    continue;
                }
                let nx = nx as usize;
                let ny = ny as usize;
                if !seen[nx][ny] && board[nx][ny] > ns {
                    q.push_back((nx, ny));
                    board[nx][ny] = ns;
                    seen[nx][ny] = true;
                }
            }
        }
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}
