N = gets.chomp.to_i
@a = {}
N.times do |i|
  @a[i] = gets.chomp.split(" ").map(&:to_i).map {|v| v - 1 }.reverse
end

@q = []
def check(i)
  return if @a[i].empty?

  j = @a[i].last
  return if @a[j].empty?

  @q << [i, j].sort if @a[j].last == i
end

N.times {|i| check(i) }

day = 0
until @q.empty?
  day += 1
  @q.uniq!
  prev_q = @q
  @q = []
  prev_q.each do |i, j|
    @a[i].pop
    @a[j].pop
  end
  prev_q.each do |i, j|
    check(i)
    check(j)
  end
end
N.times do |i|
  unless @a[i].empty?
    puts "-1"
    exit 0
  end
end
puts day
