#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| min!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(z) = it.next() {
            Some(it.fold(z, |x, y| max!(x, y)))
        } else {
            None
        }
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

#[derive(Debug)]
struct Topological(Vec<usize>);
impl Topological {

    fn visit(u: usize, mut used: &mut Vec<bool>, rd: &Vec<Vec<usize>>, mut ord: &mut Vec<usize>) {
        if used[u] { return }
        used[u] = true;
        for &v in rd[u].iter() {
            Self::visit(v, &mut used, &rd, &mut ord);
        }
        ord.push(u);
    }

    fn new(rd: &Vec<Vec<usize>>) -> Self {
        let n = rd.len();
        let mut used = vec![false; n];
        let mut ord = vec![];
        for u in 0..n { Self::visit(u, &mut used, &rd, &mut ord); }
        Topological(ord)
    }
}

fn main() {

    let mut sc = Scanner::new();
    let n: usize = sc.cin();

    use std::collections::HashMap;
    let mut task_id = HashMap::new();
    {
        let mut k = 0usize;
        for i in 0..n {
            for j in 0..i {
                task_id.insert((i, j), k);
                task_id.insert((j, i), k);
                k += 1;
            }
        }
    }

    let mut rev = vec![vec![]; task_id.len()];
    {
        for i in 0..n {
            let js: Vec<_> = (1..n).map(|_| sc.cin::<usize>()).map(|k| k - 1).collect();
            for k in 0..n-2 {
                let u = *task_id.get(&(i, js[k])).unwrap();
                let v = *task_id.get(&(i, js[k+1])).unwrap();
                rev[v].push(u);
            }
        }
    }
    // trace!(&rev);

    // let mut visited = vec![false; task_id.len()];
    // let mut ord = vec![];
    // let mut ind = vec![0; task_id.len()];
    // for u in 0..task_id.len() {
    //     if visited[u] { continue }
    //     let mut stack = vec![(false, u), (true, u)];
    //     while let Some((dir, u)) = stack.pop() {
    //         // trace!(dir, u);
    //         if dir {
    //             visited[u] = true;
    //             for &v in rev[u].iter() {
    //                 if visited[v] { continue }
    //                 stack.push((false, v));
    //                 stack.push((true, v));
    //             }
    //         } else {
    //             ind[u] = ord.len();
    //             ord.push(u);
    //         }
    //     }
    // }
    // trace!(&ord);
    // trace!(&ind);

    let Topological(ord) = Topological::new(&rev);
    let mut ind = vec![0; rev.len()];
    for (i, &x) in ord.iter().enumerate() {
        ind[x] = i;
    }

    // cycle?
    let mut ok = true;
    for u in 0..task_id.len() {
        for &v in rev[u].iter() {
            if ind[u] < ind[v] {
                ok = false;
                break;
            }
        }
    }
    if !ok {
        put!(-1);
        return;
    }

    let mut dp = vec![0; task_id.len()];
    for &u in ord.iter() {
        dp[u] = rev[u].iter().map(|&v| dp[v]).fold(1, |a, x| max!(a, 1 + x));
    }

    put!(dp.iter().max().unwrap());
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}


Measured memory_bytes: 133416