Creating the shortest_way function