def count_bags_inside(bag_name, rules):
    """Count total number of bags in bag_name."""
    if not rules[bag_name]:
        return 0
    count = 0
    for inside_bag, number_of_bags in rules[bag_name].items():
        count += (number_of_bags + number_of_bags
                  * count_bags_inside(inside_bag, rules))
    return count