27. Five friends, one of whom had a monkey, once bought a bag of nuts, which they intended to divide among themselves the next morning. However, one of the friends woke up at night and wanted nuts; he divided all the nuts in the bag into five equal parts, with one extra nut left over, which he gave to the monkey, and took one fifth for himself. Soon after, another of the nut owners woke up; not knowing that someone had already taken nuts, he divided all the remaining contents of the bag again into five parts, with one extra nut left over, which he gave to the monkey, and took one fifth for himself. Then the remaining three friends successively performed the same operation; each of them, not knowing about the actions of the others, divided all the nuts into five parts, took one fifth for himself, and each time one extra nut remained, which was given to the monkey. Finally, in the morning, all five of them together took out the bag, divided the remaining nuts into five parts, and one nut, which turned out to be extra, was again given to the monkey. It is required to determine the smallest number of nuts in the bag for which such a division is possible.
27. First solution. Let $n$ be the number of nuts each friend received in the morning; in this case, $5n + 1$ is the number of nuts that were in the bag in the morning. The last of those who woke up at night, obviously, took $\frac{5n + 1}{4}$ for himself, and before that, there were $5 \frac{5n + 1}{4} + 1 = \frac{25n + 9}{4}$ nuts in the bag. The second to last who woke up took $\frac{1}{4} \cdot \frac{25n + 9}{4}$ for himself, and before that, there were $5 \cdot \frac{1}{4} \cdot \frac{25n + 9}{4} + 1 = \frac{125n + 61}{16}$ nuts in the bag; the third took $\frac{1}{4} \cdot \frac{125n + 61}{16}$ for himself, and before that, there were $5 \cdot \frac{1}{4} \cdot \frac{125n + 61}{16} + 1 = \frac{625n + 369}{64}$ nuts in the bag; the second took $\frac{1}{4} \cdot \frac{625n + 369}{64}$ for himself, and before that, there were $5 \cdot \frac{1}{4} \cdot \frac{625n + 369}{64} + 1 = \frac{3125n + 2101}{256}$ nuts in the bag; finally, the first took $\frac{1}{4} \cdot \frac{3125n + 2101}{256}$ for himself, and initially, there were

$N = 5 \cdot \frac{1}{4} \cdot \frac{3125n + 2101}{256} + 1 = \frac{15625n + 11529}{1024} \Rightarrow$

$$
= 15n + 11 + \frac{265n + 265}{1024}
$$

nuts. Since this number must be an integer, $265(n + 1)$ must be divisible by 1024. The smallest value of $n$ that satisfies this condition is, obviously, 1023, and in this case,

$$
N = 15 \cdot 1023 + 11 + 265 = 15621
$$

Second solution. This problem can be solved much faster and almost without any calculations if we carefully consider the conditions that the content of the problem imposes on the total number $N$ of nuts. The first condition of the problem is that during the first division into five parts, one nut remains. This condition means that $N$ when divided by 5 gives a remainder of 1, i.e., $N = 5l + 1$; numbers that satisfy this condition occur in the natural sequence of numbers with an interval of five numbers, and knowing one such number, we can find an unlimited number of others by adding (or subtracting) multiples of 5. The second condition of the problem states that $k = \frac{4}{5}(N - 1) = 4l$ gives a remainder of 1 when divided by 5, i.e., $k = 5l_1 + 1$. This requirement is equivalent to $l$ giving a remainder of 4 when divided by 5 or that $N = 5l + 1$ gives a remainder of 21 when divided by 25; numbers that satisfy this condition occur in the number sequence with an interval of 25 numbers, and knowing one such number, we can obtain as many as we want by adding (or subtracting) multiples of 25. Similarly, the third condition of the problem states that $k_1 = \frac{4}{5}(k - 1) = 4l_1$ gives a remainder of 1 when divided by 5; this condition determines the remainder from the division of the number $l_1$ by 5 or the remainders from the division of the numbers $k$ and $l$ by 25, or the remainder from the division of $N$ by 125. All conditions of the problem determine the remainder from the division of the number $N$ by $5^6 = 15625$; numbers that satisfy these conditions occur in the natural sequence of numbers with an interval of 15625.

We could calculate the remainder that the number $N$ gives when divided by 58, but there is no need for this. The fact is that one number that satisfies all the conditions of the problem is obvious. This number is -4. Indeed, when -4 is divided by 5, it gives a quotient of -1 and a remainder of +1; therefore, if we subtract 1 from -4 and take $\frac{4}{5}$ of the resulting difference, which is already divisible by 5, we will get the same number -4. Similarly, in all subsequent divisions by 5, we will have the same remainder +1. True, the number -4 cannot serve as an answer to our problem, as, by condition, the number $N$ must be positive; but knowing one number that satisfies the conditions of the problem, we can obtain as many as we want by adding multiples of $5^6$. The smallest positive number that satisfies the conditions is, obviously, $-4 + 5^6 = 15625 - 4 = 15621$.