
fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val verticalLines = hashMapOf<Int, MutableSet<Pair<Int, Int>>>()
    val horizontalLines = hashMapOf<Int, MutableSet<Pair<Int, Int>>>()
    for (i in 0 until n) {
        val (a, b, c) = readLine()!!.split(" ").map { it.toInt() }
        verticalLines.getOrPut(c, { hashSetOf() }).add(a to b)
    }
    for (i in 0 until m) {
        val (d, e, f) = readLine()!!.split(" ").map { it.toInt() }
        horizontalLines.getOrPut(d, { hashSetOf() }).add(e to f)
    }

    val minY = verticalLines.map { entry -> entry.key }.min()!!
    val minX = horizontalLines.map { entry -> entry.key }.min()!!

    val maxY = verticalLines.map { entry -> entry.key }.max()!!
    val maxX = horizontalLines.map { entry -> entry.key }.max()!!

    val moved = hashSetOf(0 to 0)
    var nextMove = moved.toSet()
    while (true) {
        val next = hashSetOf<Pair<Int, Int>>()
        for ((x, y) in nextMove) {
            if (x < minX || x > maxX || y < minY || y > maxY) {
                println("INF")
                return
            }
            if (!moved.contains(x - 1 to y) && !next.contains(x - 1 to y)) {
                if (canMove(horizontalLines, y, x - 1, x)) {
                    next.add(x - 1 to y)
                }
            }
            if (!moved.contains(x + 1 to y) && !next.contains(x + 1 to y)) {
                if (canMove(horizontalLines, y, x + 1, x)) {
                    next.add(x + 1 to y)
                }
            }
            if (!moved.contains(x to y - 1) && !next.contains(x to y - 1)) {
                if (canMove(verticalLines, x, y - 1, y)) {
                    next.add(x to y - 1)
                }
            }
            if (!moved.contains(x to y + 1) && !next.contains(x to y + 1)) {
                if (canMove(verticalLines, x, y + 1, y)) {
                    next.add(x to y + 1)
                }
            }
        }
        if (next.isEmpty()) break
        moved.addAll(next)
        nextMove = next
    }
    print(moved.size)
}

fun canMove(
    horizontalLines: Map<Int, MutableSet<Pair<Int, Int>>>,
    fromX: Int,
    fromY: Int,
    toY: Int
): Boolean {
    val lines = horizontalLines[Math.min(fromY, toY)] ?: return true

    for (line in lines) {
        val (a, b) = line
        if (Math.max(a, b) >= fromX && Math.min(a, b) < fromX) {
            return false
        }
    }
    return true
}