cho.sh
Notes
Loading...

Perimeter of a Rectangle Union

Time limit

2s

Memory limit

128 MB

Problem

Given N axis-aligned rectangles on a two-dimensional plane, find the total length of the outer boundary of their union. Parts where rectangles overlap or share an edge are not counted when they become internal to the union. If N is 0, the union is empty and the answer is 0.

Input

The first line contains the number of rectangles N (0 ≤ N ≤ 5,000). Each of the next N lines contains four integers x1, y1, x2, y2. The points (x1, y1) and (x2, y2) are opposite vertices of one rectangle. Every coordinate is between -10,000 and 10,000, inclusive.

Output

Print one line containing the perimeter length of the union of the rectangles.