use std::collections::VecDeque;

use proconio::input;

fn main() {
    input! {
        n: usize,
        pp: [usize; n * n],
    };

    let mut grid = vec![vec![true; n]; n];
    let mut ans = 0;
    for p in pp {
        let x = (p - 1) / n;
        let y = (p - 1) % n;
        let mut v = VecDeque::new();
        let mut visited = vec![vec![false; n]; n];
        v.push_back((x, y, 0));
        visited[x][y] = true;
        // println!("{} {} {}", p, x, y);
        while let Some((x, y, cost)) = v.pop_front() {
            if x == 0 || y == 0 || x + 1 == n || y + 1 == n {
                ans += cost;
                break;
            }
            visited[x][y] = true;
            for &(xx, yy) in &[(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)] {
                if visited[xx][yy] {
                    continue;
                }
                if grid[xx][yy] {
                    v.push_back((xx, yy, cost + 1));
                } else {
                    v.push_front((xx, yy, cost));
                }
            }
        }
        grid[x][y] = false;
        // println!("{:?}", grid);
    }
    println!("{}", ans);
}
