This question has already been answered:
Arranged the blocks in two columns, but the last one if the last block in the current column does not fit, then it is partially transferred to the new one, how to make the blocks transferred completely, not cut into 2 columns? 