n = gets.chomp.to_i

hash = {}
(1..n).each do |i|
  hash[i] = gets.chomp.split(" ").map(&:to_i)
end

days = 0
count = 0
set = {}
while count < n do
  set = {}
  flag = true
  keys = hash.keys.reverse
  keys.each do |k|
    arr = hash[k]
    next if arr.length == 0
    pair = arr.shift
    arr2 = hash[pair]
    tmp_k = arr2.shift
    if tmp_k == k && !set[k] && !set[pair] then
      if arr.length == 0 then
        count += 1
      end
      if arr2.length == 0 then
        count += 1
      end
      flag = false
      set[k] = true
      set[pair] = true
    else
      arr.unshift(pair)
      arr2.unshift(tmp_k)
    end
  end
  if flag then
    puts "-1"
    exit 0
  end
  days += 1
end
puts days
