sub f{my($x,$n,$i)=@_;$n-1?${$_**$n>$x?last:$x%$_||f($x/$_,$n-1,$i),$i++for@d[$i..$#d]}:++$%}$_=<>;@d=grep$X%$_<1,2..sqrt($X=/ /+$');f$X,$_;print$%