require'prime' q=[] p Prime.each(2e5).grep(eval'%d..%d'%[*$<]).reverse.max_by{|a|q=a%=9,*q[0,q.index(a)||1e9] q.size}