require "prime" require "set" prime_max = 5000000 gets set = gets.split.map(&:to_i).to_set ok_set = Set.new start = 1 now_set = Set.new Prime.each(prime_max) do |prime| num_set = prime.to_s.each_char.map(&:to_i).uniq.to_set if set.superset?(num_set) now_set += num_set else if set == now_set ok_set << prime - 1 - start end start = prime + 1 now_set.clear end end if set == now_set ok_set << prime_max - start end if ok_set.empty? puts(-1) else puts(ok_set.max) end