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