Adding alignment of bounding boxes as a loss function

Hi, I currently have a model to predict bounding boxes, trained using a simple regression loss.

I’m now trying to encode the alignment of bounding boxes as a loss function, to maximize the no. of boxes that share either one of the edges.
e.g. in the image, any of the cases with green boxes are preferred, where they are sharing an edge.

How can I add this?