Ordering problem solved using A* search algorithm