use std::cmp;
use std::collections::VecDeque;
fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let p: Vec<_> = (0..(n * n)).map(|_| sc.read::<usize>() - 1).collect();
    let mut dist = vec![0; n * n];
    for x in 0..(n * n) {
        let (i, j) = (x / n, x % n);
        let h = cmp::min(i, n - 1 - i);
        let w = cmp::min(j, n - 1 - j);
        dist[x] = cmp::min(h, w);
    }

    let mut empty = vec![false; n * n];
    let mut sum = 0;
    let mut q = VecDeque::new();
    for p in p.into_iter() {
        empty[p] = true;
        let (i, j) = (p / n, p % n);
        sum += dist[p];
        for (ni, nj) in next(i, j, n).into_iter() {
            let np = ni * n + nj;
            if dist[np] > dist[p] {
                dist[np] = dist[p];
                q.push_back((ni, nj));
            }
        }

        while let Some((i, j)) = q.pop_front() {
            let p = i * n + j;
            let cost = if empty[p] { 0 } else { 1 };
            for (ni, nj) in next(i, j, n).into_iter() {
                let np = ni * n + nj;
                if dist[np] > dist[p] + cost {
                    dist[np] = dist[p] + cost;
                    q.push_back((ni, nj));
                }
            }
        }
    }
    println!("{}", sum);
}

fn next(i: usize, j: usize, n: usize) -> Vec<(usize, usize)> {
    let mut result = vec![];
    if i > 0 {
        result.push((i - 1, j));
    }
    if j > 0 {
        result.push((i, j - 1));
    }
    if i + 1 < n {
        result.push((i + 1, j));
    }
    if j + 1 < n {
        result.push((i, j + 1));
    }
    result
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
