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

N,M = gets.split.map(&:to_i)
縦線 = N.times.map{ gets.split.map(&:to_i) }
横線 = M.times.map{ gets.split.map(&:to_i) }

経線 = 縦線.map{|_,_,c| c }.uniq.sort
緯線 = 横線.map{|d,| d }.uniq.sort

GW = 経線.size+1 # 大外で壁になる線分の外側に１マス確保する。牛さんがそこにいる可能性もある。
GH = 緯線.size+1
HI = lambda{|横,下げ=false| # 0 番目のマス目の右側に 0 番目の縦線が配置される関係から、基本的に線分の座標は右へ、下へ切り上げられる。
	i = 経線.bsearch_index{|_| 横<=_ }||経線.size
	i1 = 下げ && 経線[i] != 横 ? 1 : 0
	next i-i1
}
VI = lambda{|縦,下げ=false|
	j = 緯線.bsearch_index{|_| 縦<=_ }||緯線.size
	j1 = 下げ && 緯線[j] != 縦 ? 1 : 0
	next j-j1
}
GI = lambda{|横,縦,下げ=false|
	next VI[縦,下げ]*GW + HI[横,下げ]
}

面積 = lambda{|gi|
	b,r = gi.divmod(GW)
	next Float::INFINITY unless (1...経線.size).include?(r) && (1...緯線.size).include?(b)
	next (経線[r]-経線[r-1]) * (緯線[b]-緯線[b-1])
}

縦線 = 縦線.inject(Array.new(経線.size){ [] }){|_,(a,b,c)|
	_[HI[c]] << [VI[a],VI[b,true]]
	next _
}
横線 = 横線.inject(Array.new(緯線.size){ [] }){|_,(d,e,f)|
	_[VI[d]] << [HI[e],HI[f,true]]
	next _
}

右？ = lambda{|gi|
	b,r = gi.divmod(GW)
	next r+1 < GW && ! 縦線[r].any?{|s,e| b-1 < e && s < b }
}
左？ = lambda{|gi|
	b,r = gi.divmod(GW)
	next 0 < r && ! 縦線[r-1].any?{|s,e| b-1 < e && s < b }
}
下？ = lambda{|gi|
	b,r = gi.divmod(GW)
	next b+1 < GH && ! 横線[b].any?{|s,e| r-1 < e && s < r }
}
上？ = lambda{|gi|
	b,r = gi.divmod(GW)
	next 0 < b && ! 横線[b-1].any?{|s,e| r-1 < e && s < r }
}

area = 0
Cow = GI[0,0]
Qd = [false]*(GW*GH)
Qd[Cow] = true;
Q = [Cow]
while at = Q.shift
	area += 面積[at]
	(puts'INF'; exit) if area.infinite?

	(Q<<at+1;  Qd[at+1]  = true) if 右？[at] && ! Qd[at+1]
	(Q<<at-1;  Qd[at-1]  = true) if 左？[at] && ! Qd[at-1]
	(Q<<at+GW; Qd[at+GW] = true) if 下？[at] && ! Qd[at+GW]
	(Q<<at-GW; Qd[at-GW] = true) if 上？[at] && ! Qd[at-GW]
end

p area