require 'prime' K = gets.to_i N = gets.to_i def calc_hash(x) while x >= 10 x = x.digits.sum end x end primes = Prime.each(N).to_a.select { |x| x >= K } counter = Array.new(10, 0) L = primes.size left = 0 right = 0 counter[calc_hash(primes.first)] += 1 max_len = 1 ans = primes.first conflicted = false while left <= right if right == L - 1 hash = calc_hash(primes[left]) conflicted = false if counter[hash] == 2 counter[hash] -= 1 left += 1 elsif conflicted hash = calc_hash(primes[left]) conflicted = false if counter[hash] == 2 counter[hash] -= 1 left += 1 else right += 1 hash = calc_hash(primes[right]) counter[hash] += 1 conflicted = true if counter[hash] >= 2 end if not conflicted len = right - left + 1 if max_len < len max_len = len ans = primes[left] elsif max_len == len ans = primes[left] end end end puts ans