require "prime" require "set" def num2set(num) list = Set.new while num != 0 list << num % 10 num /= 10 end return list end prime_max = 5000000 # prime_max = 5000 gets set = gets.split.map(&:to_i).to_set ok = Set.new start = 1 remain = set exist_flag = false complete_flag = false Prime.each(prime_max) do |prime| # nums = prime.to_s.each_char.map(&:to_i).to_set nums = num2set(prime) if set.superset?(nums) unless complete_flag exist_flag = true remain -= nums if remain.empty? complete_flag = true end end else if exist_flag if complete_flag ok << prime - 1 - start end remain = set complete_flag = false exist_flag = false end start = prime + 1 end end if complete_flag ok << prime_max - start end if ok.empty? puts(-1) else puts(ok.max) end