Magnetic sensor largest range

I want to put two magnets at certain intervals on a horizontal. Once the magnetic sensor first reads magnet 1 and then magnet 2 (the direction is forward), it stops performing the number of times it has been assigned by the buttons. When you first read magnet 2, then magnet 1 (the direction is back).