N=gets.to_i
as=Array.new(N+1)
as[0]=Array.new(N,nil)
for i in 1..N
    as[i]=[nil]+gets.split.map(&:to_i)
end
battle=Array.new(N+1,1)
day=0
while true
    blank=Array.new(N+1,0)
    checked=Array.new(N+1,false)
    judge=false
    day+=1
    for i in 1..N
        next if battle[i]>=N ||  checked[i]
        s=as[i][battle[i]]
        if as[s][battle[s]]==i
            judge=true
            checked[i]=true
            checked[s]=true
            battle[i]+=1
            battle[s]+=1
        end
    end
    break if !judge
end
puts (battle[1..-1].all?{|x|x==N} ? day : -1)
