stack to queue. Spent 30 minutes on the bad solution, while it's only 3 minutes for the best solution.

Should have thought about it more detaily.