require 'prime' class Calc0012 MAX = 5000000 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n = args.shift.first.to_i @as = args.shift.map(&:to_i) @prime = Prime.instance end def run primes = @prime.each(MAX).to_a as = @as.map { |a| 1 << a }.inject(:|) s = primes.map { |p| p.to_s.split(//).map(&:to_i).map { |a| 1 << a }.inject(:|) } (0...s.size).each do |i| s[i] = nil if (s[i] & ~as) != 0 end max_range = -1 i = 0 loop do i += 1 while !s[i] && i < s.size break if i == s.size t = i i += 1 while s[i] && i < s.size u = (t...i).map { |a| s[a] }.inject(:|) if u == as min = t == 0 ? 1 : primes[t - 1] + 1 max = i == s.size ? MAX : primes[i] - 1 range = max - min max_range = range if max_range < range end end max_range end end puts Calc0012.new(STDIN.readlines).run if __FILE__ == $0