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)
ans = 0

while num.min != n-1 do
    ans += 1

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

        if ans != day[i] && ans != day[opp] && i == a[opp][num[opp]] then
            num[i] += 1
            num[opp] += 1
            day[i] = ans
            day[opp] = ans
        end
    end

    if ans != day.max then
        puts -1
        exit
    end
end

puts ans