#![allow(unused)]

use proconio::input;
use proconio::marker::{Bytes, Chars, Usize1};
use std::cmp::{Reverse, min, max};
use itertools::Itertools;

const N_MAX: usize = 500;

static mut costs: [i32; (N_MAX + 2) * (N_MAX + 2)] =
    [0; (N_MAX + 2) * (N_MAX + 2)];
static mut exited: [bool; (N_MAX + 2) * (N_MAX + 2)] =
    [false; (N_MAX + 2) * (N_MAX + 2)];
static mut queue: [usize; N_MAX * N_MAX] = [0; N_MAX * N_MAX];

#[proconio::fastout]
fn main() {
    input! {
        n: usize,
        ps: [Usize1; n * n],
    }

    let mut ans = 0;

    unsafe {
        for x in 1..=n {
            for y in 1..=n {
                costs[x * (n + 2) + y] =
                    min(min(x - 1, n - x), min(y - 1, n - y)) as i32;
            }
        }

        for p in ps {
            let x = p / n;
            let y = p % n;
            let p = (x + 1) * (n + 2) + (y + 1);

            exited[p] = true;
            ans += costs[p];

            let mut nelem = 1;
            queue[0] = p;

            while nelem > 0 {
                let p1 = queue[nelem - 1];
                let c = costs[p1] + if exited[p1] { 0 } else { 1 };
                for &p2 in &[p1 - 1, p1 + 1, p1 - (n + 2), p1 + (n + 2)] {
                    if costs[p2] > c {
                        costs[p2] = c;
                        queue[nelem] = p2;
                        nelem += 1;
                    }
                }
            }
        }
    }

    println!("{}", ans);
}
