#![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;

fn main() {

    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let t: usize = n * (n - 1) / 2;

    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![]; t];
    {
        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 dp: Vec<Option<u64>> = vec![None; t];
    {
        for u in 0..t {
            if rev[u].len() == 0 {
                dp[u] = Some(1);
            }
        }
        for u in 0..t {
            if dp[u].is_some() { continue }
            let mut reserved = vec![false; t];
            let mut stack = vec![(false, u), (true, u)];
            while let Some((dir, u)) = stack.pop() {
                if dp[u].is_some() { continue }
                if dir {
                    if reserved[u] {
                        put!(-1);
                        return;
                    }
                    reserved[u] = true;
                    for &v in rev[u].iter() {
                        if dp[v].is_some() { continue }
                        stack.push((false, v));
                        stack.push((true, v));
                    }
                } else {
                    if dp[u].is_some() { continue }
                    if let Some(mx) = rev[u].iter().map(|&v| dp[v].unwrap()).max() {
                        dp[u] = Some(1 + mx);
                    } else {
                        dp[u] = Some(1);
                    }
                }
            }
        }
    }
    // trace!(&dp);
    put!(dp.iter().max().unwrap().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: 106664