# How is energy conserved in a series of magnets, progressively moving another magnet upwards?

Imagine a tall building with windows allinged one above the other, and a railway going from the bottom of the building to the top of the building (vertically), right where the windows are.

Imagine there is a magnet in this railway. Let us consider this following scenario:

A person who is holding a magnet opens the first (lowest) window. The magnet in the railway starts going up (because of the electromagnetic force). Right when the magnet of the railway reaches the first window, the person closes this window and throws his magnet to the other side. In the meantime, a person standing next to the second window opens the window and the magnet in the railway continues to accelerate upwards.

Obviously no energy is invested (if we replace the people with a fricionless mechanical system) but some mass does gain potential gravitational energy!

Obviously there is some catch here because energy is to be conserved at all time, but I failed to understand it.

Please explain why this structure is impossible or where does the energy come from.