If you're empty, start moving to the right.
Whenever you reach an object and you're empty, pick it up (duh) and move toward its destination.
Whenever you reach an object
aand you're already carryingb, always choose whichever of the objects has the numerically smallest destination (furthest to the left).If you're not yet at M, go back to step 1.
This is optimal: The only place where you have a real choice is in step 3. Handling the leftmost destination first ensures that by the time you've dispatched both objects, you'll be as far to the right as possible.
Why is this question on programmers.sx? Yes, "interview question", but it's just a nice riddle.
PS. In terms of implementation, all you need is the list of tasks (the integer pairs of points) sorted by original position.