Cut all of the bottom and upper flaps off. Cut one of the short sides of the cardboard , leaving you with 2 long sides and a short side in the middle. (when you use a square box you can make as many folds as you like, be sure to cut the foam accordingly, see follwing steps how to)
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)