Perimeter of union of rectangles

Dear Sir,
I am solving a problem and i have to find a perimeter of union of rectangles so now I am stuck at it.Can you please suggest me the way to do the same.I thought something like | P1 U P2 |= P1 + P2 - (P1 intersection P2),but how can i implement it for the same.

@satyansh1399,
This is the exact problem you are talking about, you can try reading it’s solution, they have explained it in a very intuitive manner.