use proconio::input;

fn main() {
    input! {
        n: usize,
        p: [usize; n*n],
    }
    let mut seats: Vec<Vec<_>> = (0..n).map(|_i| (0..n).map(|_j| true).collect()).collect();
    let mut table: Vec<Vec<_>> = (0..n)
        .map(|i| {
            (0..n)
                .map(|j| i.min(j).min(n - i - 1).min(n - j - 1))
                .collect()
        })
        .collect();
    let mut result = 0;
    for pi in p {
        let (i, j) = ((pi - 1) % n, (pi - 1) / n);
        seats[i][j] = false;
        result += table[i][j];
        let mut queue = std::collections::VecDeque::new();
        queue.push_back((i, j));
        while !queue.is_empty() {
            let (ii, jj) = queue.pop_front().unwrap();
            if 0 < ii && table[ii - 1][jj] > table[ii][jj] {
                let prev = table[ii - 1][jj];
                table[ii - 1][jj] = table[ii][jj] + seats[ii][jj] as usize;
                if prev > table[ii - 1][jj] {
                    queue.push_back((ii - 1, jj));
                }
            }
            if ii < n - 1 && table[ii + 1][jj] > table[ii][jj] {
                let prev = table[ii + 1][jj];
                table[ii + 1][jj] = table[ii][jj] + seats[ii][jj] as usize;
                if prev > table[ii + 1][jj] {
                    queue.push_back((ii + 1, jj));
                }
            }
            if 0 < jj && table[ii][jj - 1] > table[ii][jj] {
                let prev = table[ii][jj - 1];
                table[ii][jj - 1] = table[ii][jj] + seats[ii][jj] as usize;
                if prev > table[ii][jj - 1] {
                    queue.push_back((ii, jj - 1));
                }
            }
            if jj < n - 1 && table[ii][jj + 1] > table[ii][jj] {
                let prev = table[ii][jj + 1];
                table[ii][jj + 1] = table[ii][jj] + seats[ii][jj] as usize;
                if prev > table[ii][jj - 1] {
                    queue.push_back((ii, jj + 1));
                }
            }
        }
    }

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