N = gets.chomp.to_i
a = Array.new(N+1){Array.new(N-1)}
x = Array.new(0)
str = ""
i = 1
j = 0
while i < N
    str = gets.split
    j = 0
    while j < N-1
        a[i][j] = str[j].to_i
        j += 1
    end
    a[i].reverse!
    i += 1
end

answer = 0
f = 1
while f != 0
    f = 0
    i = 2
    while i <= N
        if !a[i].empty?
            f += 1
            b = a[i].last
            if i > b && i == a[b].last
                x.push(a[b].last)
                x.push(b)
            end 
        end
    end
    if x.size > 0
        answer += 1
    elsif f != 0
        puts -1
        exit
    end
    while !x.empty?
        a[x.pop].pop
    end
end
puts answer