#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn to_id(i: usize, j: usize, n: usize) -> usize {
    if i > j {
        i+j*n
    } else {
        j+i*n
    }
}

fn dfs(mut visited: &mut HashSet<usize>, mut dp: &mut HashMap<usize, usize>, mut calculated: &mut HashSet<usize>, nodes: &HashMap<usize, Vec<usize>>, node: usize) -> Option<usize> {
    if visited.contains(&node) {
        if !calculated.contains(&node) {
            return None;
        }
        return dp.get(&node).cloned();
    }
    visited.insert(node);
    if let Some(nexts) = nodes.get(&node) {
        for &next in nexts {
            if let Some(res) = dfs(&mut visited, &mut dp, &mut calculated, &nodes, next) {
                let entry = dp.entry(node).or_insert(1);
                *entry = max!(*entry, res+1);
            } else {
                return None;
            }
        }
    }
    calculated.insert(node);
    Some(*dp.entry(node).or_insert(1))
}

fn solve() {
    let n = get!(usize);
    let a = get!([usize]; n);
    let mut nodes = HashMap::new();
    let mut vertexes = HashSet::new();
    for i in 0..n {
        for j in 0..n-2 {
            let src = to_id(a[i][j],i+1,n);
            let dst = to_id(a[i][j+1],i+1,n);
            vertexes.insert(src);
            vertexes.insert(dst);
            nodes.entry(src).or_insert(Vec::new()).push(dst);
        }
    }
    let mut visited = HashSet::new();
    let mut dp = HashMap::new();
    let mut calculated = HashSet::new();
    let mut mx = 0;
    for v in vertexes {
        if let Some(res) = dfs(&mut visited, &mut dp, &mut calculated, &nodes, v) {
            mx = max!(mx, res);
        } else {
            println!("-1");
            return;
        }
    }
    println!("{}", mx);
}


Measured memory_bytes: 165220