N = gets.to_i
players = N.times.map { gets.split.map{|i| i.to_i - 1} }

counter = 0
break_counter = N * (N - 1) / 2
lock = Array.new(N, 0)
next_chekcable = (0..(N-1)).to_a
while true
  checkable = Marshal.load(Marshal.dump(next_chekcable))
  next_chekcable = []

  counter += 1
  flag = false

  checkable.each do |player_id|
    opponent_id = players[player_id].first
    next if opponent_id.nil?

    if players[opponent_id].first == player_id && lock[player_id] != counter && lock[opponent_id] != counter
      flag = true
      players[player_id].shift
      players[opponent_id].shift
      lock[player_id] = counter
      lock[opponent_id] = counter
      next_chekcable.push(player_id)
      next_chekcable.push(opponent_id)
      break_counter -= 1
    end
  end

  if !flag
    puts '-1'
    exit
  end

  break if break_counter.zero?
end

puts counter