Shortest path function on distance