module mod_unweighted_graph
  implicit none
  type t_list
    integer, allocatable :: at(:, :)
  end type
  type unweighted_graph
    type(t_list), allocatable :: list(:)
  end type
  interface unweighted_graph
    module procedure :: newug0
  end interface unweighted_graph
contains
  type(unweighted_graph) function newug0(n, m, a, b, e, undirected) result(res)
    integer, intent(in) :: n, m, a(:), b(:), e(:)
    logical, optional, intent(in) :: undirected
    logical :: u
    integer :: c(n), i
    u = merge(undirected, .true., present(undirected))
    c = 0
    do i = 1, m
      c(a(i)) = c(a(i)) + 1
      if (u) c(b(i)) = c(b(i)) + 1
    end do
    allocate(res%list(n))
    do i = 1, n
      allocate(res%list(i)%at(c(i), 2))
    end do
    do i = m, 1, -1
      res%list(a(i))%at(c(a(i)), 1) = b(i)
      res%list(a(i))%at(c(a(i)), 2) = e(i)
      c(a(i)) = c(a(i)) - 1
      if (u) then
        res%list(b(i))%at(c(b(i)), 1) = a(i)
        res%list(b(i))%at(c(b(i)), 2) = e(i)
        c(b(i)) = c(b(i)) - 1
      end if
    end do
  end
end module mod_unweighted_graph
module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) < b(1)
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program perils_in_parallel
  use mod_unweighted_graph
  use mod_two_dim_merge_sort
  implicit none
  integer :: n, m, cnt = 0, i, j, l, r, p
  integer :: a(100000, 2) = 0, x(200000, 3) = 0, y(200000, 1) = 0, z(100001) = 0
  logical :: used(100001) = .false.
  type(unweighted_graph) :: g
  read(*,*) n, m
  do i = 1, n
    read(*,*) a(i, :)
  end do
  call merge_sort(a(1:n, :))
  z(1) = a(1, 2)
  do i = 2, n
    z(i) = xor(a(i, 2), a(i - 1, 2))
  end do
  z(n + 1) = a(n, 2)
  do i = 1, m
    j = cnt + 1
    read(*,*) x(j, 1:2)
    x(j, 3) = i
    if (x(j, 1) > a(n, 1)) cycle
    if (x(j, 2) < a(1, 1)) cycle
    l = 0
    r = n
    do while (r - l > 1)
      p = (l + r) / 2
      if (x(j, 1) <= a(p, 1)) then
        r = p
      else
        l = p
      end if
    end do
    x(j, 1) = r
    l = 1
    r = n + 1
    do while (r - l > 1)
      p = (l + r) / 2
      if (x(j, 2) >= a(p, 1)) then
        l = p
      else
        r = p
      end if
    end do
    x(j, 2) = r
    if (x(j, 1) < x(j, 2)) cnt = cnt + 1
  end do
  m = cnt
  g = unweighted_graph(n + 1, m, x(1:m, 1), x(1:m, 2), x(1:m, 3))
  p = 0
  do i = 1, n + 1
    if (used(i)) cycle
    if (btest(dfs(g, i), 0)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') p
  if (p > 0) then
    call merge_sort(y(1:p, :))
    write(*,'(i0)',advance='no') y(1, 1)
    do i = 2, p
      write(*,'(x,i0)',advance='no') y(i, 1)
    end do
  end if
  write(*,*)
contains
  recursive integer function dfs(this, v) result(res)
    type(unweighted_graph), intent(in) :: this
    integer, intent(in) :: v
    integer :: i, u, tmp
    used(v) = .true.
    res = z(v)
    do i = 1, size(this%list(v)%at, 1)
      u = this%list(v)%at(i, 1)
      if (used(u)) cycle
      tmp = dfs(this, u)
      if (btest(tmp, 0)) then
        p = p + 1
        y(p, 1) = this%list(v)%at(i, 2)
      end if
      res = xor(res, tmp)
    end do
  end
end program perils_in_parallel

Measured memory_bytes: 26624