from typing import Dict
from typing import List
from typing import Tuple


def count_bags(
    rules: Dict[str, List[Tuple[str, int]]], bag_name: str, multiplier: int
) -> int:
    """
    Count the number of bags necessarily contained in `multipler` bags of
    type `bag_name` according to the `rules`.

    Note that this includes the outer bags themselves!
    """
    return multiplier * (
        1 + sum(count_bags(rules, name, mult) for name, mult in rules[bag_name])
    )