Finding the shortest path based on distance