switching of the material in both directions

Hello! I’m use MultiGate node for switching material, and tie it to the left and right arrows (meshes) but MultiGate allows you to switch materials either from the beginning or from the end. It is supposed to switch the material with these arrows back and forward, and i need to switch to the metarial that was previous or next and not from the last or first in the list.
Apparently need to call the material check and already on the basis of the result when you click on a particular arrow should be assigned to the necessary material. But I do not know how to implement it!

In advance very thankful!