require "set"

N = gets.chomp.to_i

a = [[]]
N.times { a << gets.chomp.split(" ").map(&:to_i) }

search_list = Set.new([*(1..N)])


days = 0
until search_list.empty?
  next_search_list = Set.new

  search_list.each do |n|
    next if a[n].empty?
    pair = a[n][0]
    next if a[pair].empty? || a[pair][0] != n
    next_search_list << n
    next_search_list << pair
  end
  next_search_list.each do |i|
    a[i].shift
  end

  days += 1
  search_list = next_search_list
end

if a.all?(&:empty?)
  puts days - 1
else
  puts "-1"
end