n = gets.to_i
a = []
n.times do |i|
    a[i] = gets.split.map{|x| x.to_i - 1}
end

num = Array.new(n,0)
day = Array.new(n,0)

while true do
    check = false

    n.times do |i|
        next if (opp = a[i][num[i]]) == nil

        if i == a[opp][num[opp]] then
            num[i] += 1
            num[opp] += 1
            d = day[i] <= day[opp] ? day[opp] : day[i]
            day[i] = d + 1
            day[opp] = d + 1
            check = true
        end
    end

    #p num,day

    if !check then
      break if num.min == n-1
        puts -1
        exit
    end
end

puts day.max
