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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.util.Queue;
import java.io.BufferedReader;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Eric
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FSingleDot solver = new FSingleDot();
        solver.solve(1, in, out);
        out.close();
    }

    static class FSingleDot {
        private int n;
        private int m;
        private int[] a;
        private int[] b;
        private int[] c;
        private int[] d;
        private int[] e;
        private int[] f;

        public void solve(int testNumber, FastReader in, PrintWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            a = new int[n];
            b = new int[n];
            c = new int[n];
            d = new int[m];
            e = new int[m];
            f = new int[m];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                b[i] = in.nextInt();
                c[i] = in.nextInt();
            }
            for (int i = 0; i < m; i++) {
                d[i] = in.nextInt();
                e[i] = in.nextInt();
                f[i] = in.nextInt();
            }

            long res = solveOther();
            out.println(res == -1 ? "INF" : res);
        }

        public long solveOther() {
            TreeMap<Integer, Integer> mapx = new TreeMap<>();
            mapx.put(0, null);
            for (int i = 0; i < n; i++) {
                mapx.put(a[i], null);
                mapx.put(b[i], null);
            }
            for (int i = 0; i < m; i++) {
                mapx.put(d[i], null);
            }
            Integer[] arr = mapx.keySet().toArray(new Integer[0]);
            int cnt = 0;
            for (Integer i : arr) {
                mapx.put(i, cnt);
                cnt++;
            }
            int[] tox = new int[mapx.size()];
            for (Integer key : mapx.keySet()) {
                tox[mapx.get(key)] = key;
            }

            TreeMap<Integer, Integer> mapy = new TreeMap<>();
            mapy.put(0, null);
            for (int i = 0; i < n; i++) {
                mapy.put(c[i], null);
            }
            for (int i = 0; i < m; i++) {
                mapy.put(e[i], null);
                mapy.put(f[i], null);
            }
            arr = mapy.keySet().toArray(new Integer[0]);
            cnt = 0;
            for (Integer i : arr) {
                mapy.put(i, cnt);
                cnt++;
            }
            int[] toy = new int[mapy.size()];
            for (Integer key : mapy.keySet()) {
                toy[mapy.get(key)] = key;
            }


            boolean[][] ngy = new boolean[tox.length + 1][toy.length + 1];
            for (int i = 0; i < n; i++) {
                int x1 = mapx.get(a[i]);
                int x2 = mapx.get(b[i]);
                int y = mapy.get(c[i]);
                for (int j = x1 + 1; j <= x2; j++) {
                    ngy[j][y] = true;
                }
            }

            boolean[][] ngx = new boolean[tox.length + 1][toy.length + 1];
            for (int i = 0; i < m; i++) {
                int x = mapx.get(d[i]);
                int y1 = mapy.get(e[i]);
                int y2 = mapy.get(f[i]);
                for (int j = y1 + 1; j <= y2; j++) {
                    ngx[x][j] = true;
                }
            }


            boolean[][] visit = new boolean[tox.length + 1][toy.length + 1];
            int fx = mapx.get(0);
            int fy = mapy.get(0);
            if (fx == 0 || fx == tox.length - 1 || fy == 0 || fy == toy.length - 1) {
                //System.out.println("INF");
                return -1;
            }

            Queue<Integer> que = new ArrayDeque<>();
            que.add(fx * 10000 + fy);
            visit[fx][fy] = true;
            while (!que.isEmpty()) {
                int cur = que.poll();
                int cx = cur / 10000;
                int cy = cur % 10000;
                // 上
                int nx = cx - 1;
                int ny = cy;
                if (!ngx[nx][ny] && !visit[nx][ny]) {
                    if (nx == 0) {
                        //System.out.println("INF");
                        return -1;
                    }
                    que.add(nx * 10000 + ny);
                    visit[nx][ny] = true;
                }
                // 下
                nx = cx + 1;
                ny = cy;
                if (!ngx[cx][ny] && !visit[nx][ny]) {
                    if (nx == tox.length) {
                        //System.out.println("INF");
                        return -1;
                    }
                    que.add(nx * 10000 + ny);
                    visit[nx][ny] = true;
                }
                // 左
                nx = cx;
                ny = cy - 1;
                if (!ngy[nx][ny] && !visit[nx][ny]) {
                    if (ny == 0) {
                        //System.out.println("INF");
                        return -1;
                    }
                    que.add(nx * 10000 + ny);
                    visit[nx][ny] = true;
                }
                // 右
                nx = cx;
                ny = cy + 1;
                if (!ngy[nx][cy] && !visit[nx][ny]) {
                    if (ny == toy.length) {
                        //System.out.println("INF");
                        return -1;
                    }
                    que.add(nx * 10000 + ny);
                    visit[nx][ny] = true;
                }
            }

            long ans = 0;
            for (int i = 1; i < tox.length; i++) {
                for (int j = 1; j < toy.length; j++) {
                    if (visit[i][j]) {
                        long dx = tox[i] - tox[i - 1];
                        long dy = toy[j] - toy[j - 1];
                        ans += dx * dy;
                    }
                }
            }
            return ans;
        }

    }

    static class FastReader {
        public BufferedReader br;
        public StringTokenizer st;
        String context = null;

        public FastReader(InputStream in) {
            this(new InputStreamReader(in));
        }

        public FastReader(InputStreamReader input) {
            br = new BufferedReader(input);
        }

        public String next() {
            if (context != null) {
                System.err.print("[" + context + "] Wait for input ...");
            }
            while (st == null || !st.hasMoreElements()) {
                try {
                    String s = br.readLine();
                    if (s == null)
                        return null;
                    st = new StringTokenizer(s);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Could not read");
                }
            }
            String res = st.nextToken();
            if (context != null) {
                System.err.println("[" + context + "] ... received : " + res);
            }
            return res;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}