require 'prime' def main k = gets.to_i n = gets.to_i primes = Prime.take_while { |p| p <= n }.select { |p| k <= p } prime_hashes = primes.map { |p| [p, p % 9] } # prime_hashが入る queue = [] max_length = 0 max_head = nil prime_hashes.each do |item| index_of_item_val = queue.index { |_item| _item[1] == item[1] } queue = queue[(index_of_item_val + 1)..-1] if index_of_item_val queue << item if max_length <= queue.size max_head = queue[0][0] max_length = queue.size end end max_head end puts main