#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let p = read_vec::<usize>()
        .iter()
        .map(|x| x - 1)
        .collect::<Vec<_>>();

    let mut grid = vec![vec![0; n]; n];
    for y in 0..n {
        for x in 0..n {
            grid[y][x] = vec![x, y, n - 1 - x, n - 1 - y].into_iter().min().unwrap() as i32;
        }
    }
    let mut occ = vec![vec![1; n]; n];

    let mut ans = 0i32;
    for num in p {
        let x = num % n;
        let y = num / n;
        ans += grid[y][x];
        occ[y][x] = 0;
        dfs(x, y, &mut grid, &mut occ);
    }
    println!("{}", ans);
}

fn dfs(x: usize, y: usize, grid: &mut Vec<Vec<i32>>, occ: &mut Vec<Vec<i32>>) {
    for (adj_x, adj_y) in get_adjacents(x, y, grid.len(), grid.len()) {
        if grid[adj_y][adj_x] > grid[y][x] + occ[y][x] {
            grid[adj_y][adj_x] = grid[y][x] + occ[y][x];
            dfs(adj_x, adj_y, grid, occ);
        }
    }
}

fn get_adjacents(x: usize, y: usize, w: usize, h: usize) -> Vec<(usize, usize)> {
    let mut adjacents = vec![];
    if x > 0 {
        adjacents.push((x - 1, y));
    }
    if y > 0 {
        adjacents.push((x, y - 1));
    }
    if y < h - 1 {
        adjacents.push((x, y + 1));
    }
    if x < w - 1 {
        adjacents.push((x + 1, y));
    }
    adjacents
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
