It has been established that, owing to the proximity of a resonance with Jupiter, Mercury's eccentricity can be pumped to values large enough to allow collision with Venus within 5Gyr (refs 1-3). This conclusion, however, was established either with averaged equations that are not appropriate near the collisions or with non-relativistic models in which the resonance effect is greatly enhanced by a decrease of the perihelion velocity of Mercury. In these previous studies, the Earth's orbit was essentially unaffected. Here we report numerical simulations of the evolution of the Solar System over 5Gyr, including contributions from the Moon and general relativity. In a set of 2,501 orbits with initial conditions that are in agreement with our present knowledge of the parameters of the Solar System, we found, as in previous studies, that one per cent of the solutions lead to a large increase in Mercury's eccentricity-an increase large enough to allow collisions with Venus or the Sun. More surprisingly, in one of these high-eccentricity solutions, a subsequent decrease in Mercury's eccentricity induces a transfer of angular momentum from the giant planets that destabilizes all the terrestrial planets ~3.34Gyr from now, with possible collisions of Mercury, Mars or Venus with the Earth.