require 'prime' def f(n) re=0 n.to_s.chars{|e| re+=e.to_i } return re end d=gets.to_i u=gets.to_i x=Prime.each(u).to_a while x[0]9 e2=f(e2) end [e,e2] } anslen=1 ans=x[0] while x.empty? == false e=x.shift e0=e[0] e1=e[1] hs={} hs[e1]=0 len=1 x.each{|e2| if hs.key?(e2[1])==false len+=1 hs[e2[1]]=0 else break end } if anslen<=len ans=e0 anslen=len end end puts ans