Electric control of magnetism and magnetic control of ferroelectricity can improve the energy efficiency of magnetic memory and data-processing devices1. However, the necessary magnetoelectric switching is hard to achieve, and requires more than just a coupling between the spin and the charge degrees of freedom2–5. Here we show that an application and subsequent removal of a magnetic field reverses the electric polarization of the multiferroic GdMn2O5, thus requiring two cycles to bring the system back to the original configuration. During this unusual hysteresis loop, four states with different magnetic configurations are visited by the system, with one half of all spins undergoing unidirectional full-circle rotation in increments of about 90 degrees. Therefore, GdMn2O5 acts as a magnetic crankshaft that converts the back-and-forth variations of the magnetic field into a circular spin motion. This peculiar four-state magnetoelectric switching emerges as a topologically protected boundary between different two-state switching regimes. Our findings establish a paradigm of topologically protected switching phenomena in ferroic materials.