def ascan; gets.split.map(&:to_i); end def scan; gets.to_i; end require 'prime' k,n = [gets,gets].map(&:to_i) # 素数列挙 prime_generator = Prime::EratosthenesGenerator.new primes = [] begin ; primes << prime_generator.next ; end while primes[-1]<=n primes.pop if primes[-1]>n def ash(u) u<10 ? u : ash(ash(u/10)+u%10) end ptr = 0 while primes[ptr] < k ptr+=1 end li = Array.new(10) q = [] best = 0 best_arr = nil while ptr < primes.size v = primes[ptr] h = ash(v) while li[h] r = q.shift() li[r] = nil end li[h] = v q.push(h) if best <= q.size best = q.size best_arr = q.map{|i| li[i]} end ptr+=1 end p best_arr[0]