program i_hate_shortest_path_problem
  implicit none
  integer :: h, w, a, b, x = 1, i
  read(*,*) h, w
  do i = 1, h
    read(*,*) a, b
    if (a <= x .and. x <= b) then
      x = b + 1
    end if
    write(*,'(i0)') merge(-1, x + i - 1, x > w)
  end do
end program i_hate_shortest_path_problem