def max_children_with_eight_dollars(money, children):
   
    if money < children:
        return -1
    
    max_eight_dollar_children = min(children, money // 8)
    
    for k in range(max_eight_dollar_children, -1, -1):
       
        remaining_money = money - (k * 8)
       
        remaining_children = children - k
        
        if remaining_money >= remaining_children and (remaining_money * remaining_children != 4 or remaining_money == 1 * remaining_children):
            return k
    
    return -1

# أمثلة للاستخدام
print(max_children_with_eight_dollars(20, 3)) 
print(max_children_with_eight_dollars(16, 2))  
print(max_children_with_eight_dollars(8, 2)) 