program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n, edge(4)
    integer(int32):: x,y,a,i

    edge(:) = 0
    read*, edge(2), edge(4), n

    do i = 1, n
        read*, x,y,a
        select case (a)
            case(1)
                edge(1) = max(edge(1),x)
            case(2)
                edge(2) = min(edge(2),x)
            case(3)
                edge(3) = max(edge(3),y)
            case(4)
                edge(4) = min(edge(4),y)
        end select
    end do

    if (edge(2) >= edge(1) .and. edge(4) >= edge(3)) then
        print'(i0)', (edge(2)-edge(1))*(edge(4)-edge(3))
    else
        print'(i0)', 0
    end if
end program name