sub f{($y=$d[$_])**$n*$y>$x?last:$x%$y||f(local$x=$x/$y)for$_..@d*--$n;$%+=!$n++}@d=grep{$x%$_<1}2..4e4,$x=<>=~$"+$';f$n=$`;print$%