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 C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        try {
            new Program().Solve();
        } catch {
            Console.WriteLine("INF");
        }
    }

    void Solve()
    {
        var nm = ReadLongArray();
        var n = nm[0];
        var m = nm[1];

        var a = new long[n]; // x
        var b = new long[n]; // x
        var c = new long[n]; // y

        for (var k = 0; k < n; k++) {
            var abc = ReadLongArray();
            a[k] = abc[0];
            b[k] = abc[1];
            c[k] = abc[2];
        }

        var d = new long[m]; // x
        var e = new long[m]; // y
        var f = new long[m]; // y

        for (var i = 0; i < m; i++) {
            var def = ReadLongArray();
            d[i] = def[0];
            e[i] = def[1];
            f[i] = def[2];
        }

        var x = new List<long>(); // x[idx] = 座標
        x.AddRange(a);
        x.AddRange(b);
        x.AddRange(d);
        x.Sort();
        x = x.Distinct().ToList();

        var y = new List<long>();
        y.AddRange(c);
        y.AddRange(e);
        y.AddRange(f);
        y.Sort();
        y = y.Distinct().ToList();

        var tate = new bool[x.Count - 1, y.Count];
        for (var i = 0; i < y.Count; i++) {
            for (var j = 0; j < x.Count - 1; j++) {
                for (var k = 0; k < n; k++) {
                    if (y[i] == c[k] && x[j] >= a[k] && x[j + 1] <= b[k]) tate[j, i] = true;
                }
            }
        }

        var yoko = new bool[x.Count, y.Count - 1];
        for (var i = 0; i < y.Count - 1; i++) {
            for (var j = 0; j < x.Count; j++) {
                for (var k = 0; k < m; k++) {
                    if (x[j] == d[k] && y[i] >= e[k] && y[i + 1] <= f[k]) yoko[j, i] = true;
                }
            }
        }

        var st = new Point(0, 0);
        for (; x[st.X] < 0; st.X++) ;
        for (; y[st.Y] < 0; st.Y++) ;

        var que = new Queue<Point>();
        que.Enqueue(st);

        var gone = new HashSet<Point>();
        var size = 0L;

        while (que.Count != 0) {
            var p = que.Dequeue();
            if (!gone.Add(p)) continue;
            if (!yoko[p.X + 1, p.Y]) que.Enqueue(new Point(p.X + 1, p.Y));
            if (!yoko[p.X + 0, p.Y]) que.Enqueue(new Point(p.X - 1, p.Y));
            if (!tate[p.X, p.Y + 1]) que.Enqueue(new Point(p.X, p.Y + 1));
            if (!tate[p.X, p.Y + 0]) que.Enqueue(new Point(p.X, p.Y - 1));
            size += (x[p.X + 1] - x[p.X]) * (y[p.Y + 1] - y[p.Y]);
        }

        Console.WriteLine(size);
    }

    #region Console.Read 関係
    long[] ReadLongArray() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
    long ReadLong() => long.Parse(Console.ReadLine());
    #endregion
}

class Point
{
    public int X;
    public int Y;
    public Point(int x, int y)
    {
        X = x; Y = y;
    }
    public override bool Equals(object obj)
    {
        var p = obj as Point;
        return p.X == X && p.Y == Y;
    }
    public override int GetHashCode()
    {
        return X.GetHashCode() ^ (5000 - Y).GetHashCode();
    }
}