find the shortest way to the end