This would be a bad day on Earth.
Your precondition that “All humanity is united in this effort” is a bit ambiguous and leaves open lots of interpretations, some that are severely unrealistic and others that are pretty dire. If the entire world were suddenly made aware of a concrete ticking clock on their lives, society would very likely collapse in short order.
Considering a Technical Estimate
In the extremely unrealistic scenario that the entire human population is willing to throw its combined daily might behind saving the species, we would need to reach the minimum viable population for the human species. Technical estimates seem to vary on this widely, but sources seem to indicate somewhere between 100-200 people, with massive social engineering, might be enough to repopulate. I think it’s pretty important to note here: there would be absolutely nothing fun about being in this group of people. Some might jump to say that the wealthy would all blast off leaving us behind, but the quality of life of these people is going to be devastatingly different for generations. People with strong skills and favorable genetic backgrounds will be required, and women are likely to have an extremely unpleasant time, since their overwhelming priority will be to safely give birth to children from a variety of different genetic backgrounds for as long as they can. This isn’t a picnic, and your duties aren’t going to be optional.
Launching 200 people to Mars with an unlimited budget and human participation would neither be particularly challenging nor all that realistic, which is why I don’t want to focus my estimates on the technical aspect. Instead, I want to look at things at a more social level.
Smallest X: 60 years
As mentioned earlier, when the entire human species becomes convinced that their extermination is imminent, there will be absolute chaos. Even the best intentioned people will have an extremely difficult time creating a minimum viable population on Mars or elsewhere if society has collapsed. Therefore, the best chance for a small X is to place the end date after the influential portion of the population (ages 16 and up) are likely to be dead. Parents will be terrified for their children and the birthrate will plummet. But the people who need to keep doing their jobs no longer have an imminent threat to their lives that will destroy social structure. Sixty years seems like a fairly stable timeframe for this situation. For today’s youth in first world countries, 80 years would be seen as a decent, if not good, life. With such a time limit imposed, it is not only feasible to set up a strong and redundant system of MVP colonies, but you could actually rely on today’s social fabric to help you do it.
Largest X: Thousands of years
This estimation is much more nebulous, because if you’re talking about smoothly transitioning the entire species off of Earth it’s going to be a much different ballgame. Moving that many people into space really isn’t feasible with current resources and technology, so the more time you can provide for technological progress, the better. It’s also interesting to consider whether or not anyone would care if such a large time limit was imposed. This would likely alarm scientists, but in many ways a limit on a scale of millennia is comparable to the threat of climate change or even the eventual death of our sun. If it’s too far off or too gradual, it simply doesn’t impact a human life on the order of decades. If the aliens are really that technologically advanced and care that much about the human species, they should be offering their help to relocate us like responsible stellar neighbors.