def count_bags(bags, bag):
    """Recursively count the bags of each type."""
    if not bags.get(bag):
        return 1
    else:
        counter = 0
        for bag_a in bags[bag]:
            counter += count_bags(bags, bag_a) * bags[bag][bag_a]
        return counter + 1