put the box flat on the ground. Put the foam on top of it and check if everything is covered. (I had 1 M�, which was plenty for my 2x 40x40 cm and 15x40 box)
Start by marking the foam where the folds of the cardboard are using the marker.
Now measure the short side of the cardboard and the thickness of the foam (in my case 2 cm). Make sure you leave twice the thickness (in my case 4 cm) of the foam as spare space so the longer sides of the cardboard have space to move around. (see picture)