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 Rust (1.42.0) (judge verdict: Wrong Answer)

macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
pub trait Bisect: Copy {
    fn halve(self, other: Self) -> Self;
    fn section_end(self, other: Self) -> bool;
}
pub mod bisect_impl {
    use super::*;
    macro_rules! impl_bisect_unsigned {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    if self > other {
                        other + (self - other) / 2
                    } else {
                        self + (other - self) / 2
                    }
                }
                fn section_end(self, other: Self) -> bool {
                    (if self > other {
                        self - other
                    } else {
                        other - self
                    }) <= 1
                }
            }
        };
    }
    macro_rules! impl_bisect_signed {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    (self + other) / 2
                }
                fn section_end(self, other: Self) -> bool {
                    (self - other).abs() <= 1
                }
            }
        };
    }
    macro_rules! impl_bisect_float {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    (self + other) / 2.
                }
                fn section_end(self, other: Self) -> bool {
                    (self - other).abs() <= 1e-8
                }
            }
        };
    }
    impl_bisect_unsigned!(u8);
    impl_bisect_unsigned!(u16);
    impl_bisect_unsigned!(u32);
    impl_bisect_unsigned!(u64);
    impl_bisect_unsigned!(usize);
    impl_bisect_signed!(i8);
    impl_bisect_signed!(i16);
    impl_bisect_signed!(i32);
    impl_bisect_signed!(i64);
    impl_bisect_signed!(isize);
    impl_bisect_float!(f32);
    impl_bisect_float!(f64);
}
pub fn binary_search<T: Bisect, F: Fn(T) -> bool>(f: F, ok: T, err: T) -> T {
    let mut ok = ok;
    let mut err = err;
    while !ok.section_end(err) {
        let m = ok.halve(err);
        if f(m) {
            ok = m;
        } else {
            err = m;
        }
    }
    ok
}
pub fn lower_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
    binary_search(|i| v[i as usize] >= x, v.len() as i64, -1) as usize
}
pub fn upper_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
    binary_search(|i| v[i as usize] > x, v.len() as i64, -1) as usize
}
#[derive(Clone, Debug)]
pub struct Compress<T> {
    v: Vec<T>,
}
impl<T: Bisect + Ord> Compress<T> {
    pub fn get(&self, x: T) -> usize {
        lower_bound(&self.v, x)
    }
}
impl<T> Compress<T> {
    pub fn len(&self) -> usize {
        self.v.len()
    }
}
impl<T> std::ops::Index<usize> for Compress<T> {
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output {
        &self.v[index]
    }
}
impl<T: Ord> std::iter::FromIterator<T> for Compress<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Compress<T> {
        use std::collections::BTreeSet;
        let v = iter
            .into_iter()
            .collect::<BTreeSet<T>>()
            .into_iter()
            .collect::<Vec<T>>();
        Compress { v: v }
    }
}
fn main() {
    input!(n, m, abc: [(i64, i64, i64); n], def: [(i64, i64, i64); m]);
    let mut ans = 0i64;
    let mut xs = vec![-2_001_001_001, 0, 2_001_001_001];
    let mut ys = vec![-2_001_001_001, 0, 2_001_001_001];
    for &(a, b, c) in &abc {
        xs.push(a * 2);
        xs.push(b * 2);
        xs.push(b * 2 + 1);
        ys.push(c * 2);
        ys.push(c * 2 + 1);
    }
    for &(d, e, f) in &def {
        xs.push(d * 2);
        xs.push(d * 2 + 1);
        ys.push(e * 2);
        ys.push(f * 2);
        ys.push(f * 2 + 1);
    }
    let xs: Compress<_> = xs.into_iter().collect();
    let ys: Compress<_> = ys.into_iter().collect();
    let mut vis = vec![vec![false; ys.len()]; xs.len()];
    let mut on = vec![vec![false; ys.len()]; xs.len()];
    for &(a, b, c) in &abc {
        let x1 = xs.get(a * 2);
        let x2 = xs.get(b * 2);
        let y = ys.get(c * 2);
        for i in x1..=x2 {
            on[i][y] = true;
        }
    }
    for &(d, e, f) in &def {
        let x = xs.get(d * 2);
        let y1 = ys.get(e * 2);
        let y2 = ys.get(f * 2);
        for j in y1..=y2 {
            on[x][j] = true;
        }
    }
    let (s, t) = (xs.get(0), ys.get(0));
    vis[s][t] = true;
    let mut stack = vec![(s, t)];
    while let Some((s, t)) = stack.pop() {
        const D: [isize; 5] = [1, 0, -1, 0, 1];
        for k in 0..4 {
            let x = s as isize + D[k];
            let y = t as isize + D[k + 1];
            if 0 <= x && x < xs.len() as isize && 0 <= y && y < ys.len() as isize {
                let x = x as usize;
                let y = y as usize;
                if !vis[x][y] && !on[x][y] {
                    vis[x][y] = true;
                    stack.push((x, y));
                }
            }
        }
    }
    for i in 1..xs.len() - 1 {
        if xs[i] % 2 == 0 {
            continue;
        }
        let pi = i - 1;
        let ni = i + 1;
        for j in 1..ys.len() - 1 {
            if ys[j] % 2 == 0 {
                continue;
            }
            let pj = j - 1;
            let nj = j + 1;
            if vis[i][j] {
                ans += (xs[ni] - xs[pi]) / 2 * (ys[nj] - ys[pj]) / 2;
            }
        }
    }
    let fail = || {
        println!("INF");
        std::process::exit(0);
    };
    for i in 0..xs.len() {
        if !on[i][0] && vis[i][0] {
            fail();
        }
        if !on[i][ys.len() - 1] && vis[i][ys.len() - 1] {
            fail();
        }
    }
    for j in 0..ys.len() {
        if !on[0][j] && vis[0][j] {
            fail();
        }
        if !on[xs.len() - 1][j] && vis[xs.len() - 1][j] {
            fail();
        }
    }
    println!("{}", ans);
}