n = gets.to_i
a = []
n.times do |i|
  a << gets.split.map(&:to_i).map { |x| x - 1 }
end
day = 0
loop do
  cnt = 0
  players = Array.new(n)
  n.times do |i|
    unless players[i]
      next if a[i].empty?
      j = a[i][0]
      if j && a[j][0] == i && !players[j]
        a[i].shift
        a[j].shift
        players[i] = true
        players[j] = true
        cnt += 1
      end
    end
  end
  if cnt == 0
    puts -1
    exit
  end
  day += 1
  if a.map(&:empty?).all?
    puts day
    exit
  end
end
