Optimal solution to a problem of exchange

If I had two red boxes and two blue boxes stacked so that the first column is red then blue, the second column is blue then red and the third column is empty (space to move)
The minimum number of strokes for it to end with all the reds in one stack and all the blue ones in the other, while allowing to stack a maximum of 3 at a time, is 3 .
What is the generic method of solving this problem? Is there an algorithm?
Thank you