require"prime" n=gets.to_i dp=Array.new(n+1) dp[0]=0 max=temp=0 Prime.each(n){|p| 0.upto(max){|k| next unless dp[k] temp=k+p break if temp>n+1 dp[temp]=dp[k]+1 unless dp[temp] } max=temp } p dp[n]||-1