n = gets.to_i
@a = [[]] + n.times.map {gets.split.map(&:to_i)}
@d = Array.new(n + 1, 0)
@c = Array.new(n + 1)

def calc(i)
    if @c[i]
        puts -1
        exit
    end

    @c[i] = 0
    j = @a[i][0]

    calc(j) while i != @a[j][0]

    @c[i] = nil
    m = [@d[i], @d[j]].max
    @d[i] = @d[j] = m + 1
    @a[i].shift
    @a[j].shift
end

n.times do |i|
    calc(i) until @a[i].empty?
end

p @d.max