n=gets.chomp.to_i
a=Array.new
for i in 0..n-1
  a[i]=gets.chomp.split(" ").map{|s| s.to_i}
end
le=[]
for i in 0..n-1
  le[i]=[a[i][0],0]
end

ans=1
while true
  ud=[] 
  for i in 0..n-1
    t=le[i][0]-1 
    if i+1==le[t][0] 
      ud.push(i)
    end
  end
  if ud[0]==nil
    puts (-1)
    break
  else
    ud.each do |i|
      q=le[i][1] 
      le[i]=[a[i][q+1],q+1] if le[i][1]!=n-2
    end
  end

  ans+=1
  f=true
  if ans>=n-5
    for i in 0..n-1
      if le[i][1]!=n-2
        f=false
        break
      end
    end
  else
    f=false
  end

  if f
    puts ans
    break
  end
end