Problem:
Score: 600 points

Problem Statement

There is a grass field that stretches infinitely.

In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0).

There are also N north-south lines and M east-west lines drawn on the field. The i-th north-south line is the segment connecting the points (A_i, C_i) and (B_i, C_i), and the j-th east-west line is the segment connecting the points (D_j, E_j) and (D_j, F_j).

What is the area of the region the cow can reach when it can move around as long as it does not cross the segments (including the endpoints)? If this area is infinite, print INF instead.

Constraints

All values in input are integers between -10^9 and 10^9 (inclusive).

1 \leq N, M \leq 1000

A_i < B_i\ (1 \leq i \leq N)

E_j < F_j\ (1 \leq j \leq M)

The point (0, 0) does not lie on any of the given segments.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
:
A_N B_N C_N
D_1 E_1 F_1
:
D_M E_M F_M

Output

If the area of the region the cow can reach is infinite, print INF; otherwise, print an integer representing the area in \mathrm{cm^2}.

(Under the constraints, it can be proved that the area of the region is always an integer if it is not infinite.)

Sample Input 1

5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4

Sample Output 1

13

The area of the region the cow can reach is 13\ \mathrm{cm^2}.

Sample Input 2

6 1
-3 -1 -2
-3 -1 1
-2 -1 2
1 4 -2
1 4 -1
1 4 1
3 1 4

Sample Output 2

INF

The area of the region the cow can reach is infinite.

Solution in Ruby (2.7.1) (judge verdict: Accepted)

# Studying https://atcoder.jp/contests/abc168/tasks/abc168_f
#      and https://atcoder.jp/contests/abc168/submissions/13393878

N,M = gets.split.map(&:to_i)
縦線,横線 = [[N,0,1,2],[M,1,2,0]].map{|n,*va|
	n.times.map{ gets.split.map(&:to_i).values_at(*va) }.group_by{_3}
}
経線,緯線 = [縦線,横線].map{|l|
	[-Float::INFINITY] + l.keys.sort + [Float::INFINITY]
}
右？,下？ = [[縦線,経線,緯線],[横線,緯線,経線]].map{|l1,l2,l3|
	Hash.new{|h,k|
		h[k] = l1[l2[k+1]].inject([true]*(l3.size-2)){|a,(b,c,_)|
			l = l3.bsearch_index{|_| b<=_ }
			r = l3.bsearch_index{|_| c<_ }-1
			next a.fill(false,l...r)
		}
	}
}

area = 0
cow = [経線,緯線].map{|l| l.bsearch_index{|_| 0<_ }-1 }
Q = [cow]
Qd = Array.new(経線.size-1){ [false]*(緯線.size-1) }
Qd[cow[0]][cow[1]] = true
until Q.empty?
	横,縦 = Q.pop

	area += (経線[横+1]-経線[横])*(緯線[縦+1]-緯線[縦])
	break if area.infinite?

	(Q << [横+1,縦]; Qd[横+1][縦] = true) if ! Qd[横+1][縦] && 右？[横][縦]
	(Q << [横-1,縦]; Qd[横-1][縦] = true) if ! Qd[横-1][縦] && 右？[横-1][縦]
	(Q << [横,縦+1]; Qd[横][縦+1] = true) if ! Qd[横][縦+1] && 下？[縦][横]
	(Q << [横,縦-1]; Qd[横][縦-1] = true) if ! Qd[横][縦-1] && 下？[縦-1][横]
end

puts(area.finite? ? area : 'INF')