Fence Cuts Farmer Melissa has tired of her cows escaping day after day from the pastures, and thus has contracted a low-bidding fence company to fence the pastures. She was, however, disappointed by the results, since the fences kept the cows from traveling from pasture to pasture, which keeps them happiest! Each of the N (1 <= N <= 500) fences is parallel to the X or Y axis and might intersect anywhere along their length - including at endpoints of perpendicular lines but not collinear ones. However, these fence segments never overlap. Consider these two examples: Fences A-B and C-D are allowed Fences A-B and C-D overlap, so this +C is not allowed, even if the fences A | B were A-C, C-B, B-D, their endpoints +-----+----+ would overlap illegally | | A C B D +D +-----+-----+-----+ Each fence has a cost C_i (1 <= C_i <= 100,000) to cut through it once (if you make k cuts in a fence you pay k times this cost). Help FM compute the minimum cost required to allow the cows to travel anywhere in the plane of the pastures. Fence i's location is described by two ordered pairs (X1_i, Y1_i) and (X2_i, Y2_i) that are the locations of its endpoints such that (-200,000 <= X1_i <= 200,000; -200,000 <= Y1_i <= 200,000; -200,000 <= X2_i <= 200,000; -200,000 <= Y2_i <= 200,000) PROBLEM NAME: fcuts INPUT FORMAT: * Line 1: A single integer: N * Lines 2..N+1: Line i+1 describes fence i with five integers: X1_i, Y1_i, X2_i, Y2_i, and C_i. SAMPLE INPUT: 5 -10 10 10 10 1 -10 -10 10 -10 2 -5 -15 -5 15 3 0 -15 0 15 4 5 -15 5 15 5 INPUT DETAILS: Below is a map of the fences (each '+' is 5 units from its nearest neighbors; '0' marks the origin): + + +4>| |<5+ + | | + +3>| | | 1 + | | | v + ---+--+--+--- + | | | + + | | | + + | | | + + | 0 | + + | | | + + | | | 2 + | | | v + ---+--+--+--- + | | | + + | | | + + OUTPUT FORMAT: * Line 1: A single integer that is the minimum cost to repair the fences so the cows have access to the entire plane that is the pasture. SAMPLE OUTPUT: 2 OUTPUT DETAILS: One solution is to make two cuts in the first fence with cost 1. One cut can be made anywhere between x=-5 and x=0 and the other between x=0 and x=5.