require 'prime' k,n = STDIN.map(&:to_i) a = [] b = [] max = 0 ans = 0 k.upto(n){|x| unless x.prime? next end a.push(x) b.push(x % 9) tmp = c = 0 (-1).downto(-9){|i| unless b[i] break end if tmp[b[i]] == 0 tmp |= (1 << b[i]) c += 1 else break end } if max <= c max = c ans = a[-c] end } p ans