#![allow(unused)]

use std::cmp::min;
use itertools::Itertools;

//
// Based on EbTech's Scanner https://stackoverflow.com/q/56449027
//
struct Scanner<'a, R: std::io::BufRead> {
    reader: R,
    buf: String,
    iter: std::str::SplitWhitespace<'a>,
}

impl<'a, R: std::io::BufRead> Scanner<'a, R> {
    fn new(reader: R) -> Self {
        Self { reader, buf: String::new(), iter: "".split_whitespace() }
    }

    fn get_str(&mut self) -> &str {
        if let Some(t) = self.iter.next() {
            return t;
        }
        self.buf.clear();
        self.reader.read_line(&mut self.buf).expect("I/O error");
        self.iter = unsafe { std::mem::transmute(self.buf.split_whitespace()) };
        self.get_str()
    }

    fn get<T: std::str::FromStr>(&mut self) -> T {
        self.get_str().parse().ok().expect("Parse error")
    }
}

const N_MAX: usize = 500;

static mut PS: [usize; N_MAX * N_MAX] = [0; N_MAX * N_MAX];
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() {
    let stdin = std::io::stdin();
    let mut sc = Scanner::new(stdin.lock());

    unsafe {
        let n: usize = sc.get();
        for i in 0..(n * n) {
            PS[i] = sc.get::<usize>() - 1;
        }

        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;
            }
        }

        let mut ans = 0;
        for i in 0..(n * n) {
            let p = PS[i];
            dbg!(p);

            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 {
                nelem -= 1;
                let p1 = QUEUE[nelem];

                let c = COSTS[p1] + if EXITED[p1] { 0 } else { 1 };

                let p2 = p1 - 1;
                if COSTS[p2] > c {
                    COSTS[p2] = c;
                    QUEUE[nelem] = p2;
                    nelem += 1;
                }

                let p2 = p1 + 1;
                if COSTS[p2] > c {
                    COSTS[p2] = c;
                    QUEUE[nelem] = p2;
                    nelem += 1;
                }

                let p2 = p1 - (n + 2);
                if COSTS[p2] > c {
                    COSTS[p2] = c;
                    QUEUE[nelem] = p2;
                    nelem += 1;
                }

                let p2 = p1 + (n + 2);
                if COSTS[p2] > c {
                    COSTS[p2] = c;
                    QUEUE[nelem] = p2;
                    nelem += 1;
                }
            }
        }

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