4. Masha has 1000 beads in 50 different colors, 20 beads of each color. What is the smallest $n$ such that for any way of making a necklace from all the beads, one can choose $n$ consecutive beads, among which there are beads of 25 different colors?
Answer: $n=462$.

First solution. Let's call a segment of the necklace of length $m$ a set of $m$ consecutive beads. If the beads in the necklace are arranged in groups of 20 of the same color, then a segment of length 461 cannot contain more than 24 different colors. Therefore, $n \geqslant 462$. Consider a segment of the necklace of length 462. Suppose that in this segment, 25 beads of different colors do not appear. Number the beads of the necklace counterclockwise so that the first beads are those of the chosen segment. Let $m$ be the number of the first bead of a color that is not present in the segment (say, yellow). We will show that the segment of the necklace from $m-461$ to $m$ contains 25 colors. Indeed, the yellow color is present in it. In the remaining part of the segment, the yellow color cannot appear by construction. But there are beads of at least 24 colors in it, since $461 > 23 \cdot 20$.

Second solution. Let's call a segment of the necklace 462 consecutive beads. We will show that in some segment, there will be 25 different colors. Suppose the opposite. Then in any segment, there are no more than 24 different colors. Consider all possible pairs consisting of a segment of the necklace and some color present in it. In each segment, there are no more than 24 different colors, and the total number of segments of the necklace is 1000. Therefore, the number of pairs does not exceed $24 \cdot 1000 = 24000$. Now consider some specific color (say, blue). There are 20 blue beads in total, and between the farthest blue beads $A$ and $B$, there are always at least 18 beads (exactly 18 only when all blue beads are consecutive). Thus, there are at least 19 segments containing $B$ and not containing $A$. The bead $A$ itself is included in 462 segments. Therefore, blue beads are contained in at least 481 segments. These arguments are valid for any color. Therefore, the total number of pairs is at least $481 \cdot 50 = 24050 > 24000$, which is impossible.