n = gets.to_i
arr = [*(1..n)].map { |_| gets.chomp.split(' ').map(&:to_i) }

days = 0
iarr = [0] * n
indexes = [*(0..n-1)]
loop do
  break if iarr.none?
  match = indexes.select { |i|
    index = iarr[i]
    if index
      target = arr[i][index] - 1
      arr[target][iarr[target]] == i + 1
    end
  }
  if match.empty?
    days = -1
    break
  end
  days += 1
  match.each do |i|
    newindex = iarr[i] + 1
    iarr[i] = newindex == n - 1 ? nil : newindex
  end
end

puts days