shortest_distance(): fix `max_dist` logic

This fixes issue #734
3 jobs for master