The millipede Mirka consists of a head and several segments, and on each segment, she has one pair of legs. When it got cold, she decided to get dressed. Therefore, she put on a sock on the left leg of the third segment from the end and then on every third segment thereafter. Similarly, she put on a sock on the right leg of the fifth segment from the end and then on every fifth segment thereafter. Then she found out that on 14 segments, both legs remained bare.

Determine how many legs the millipede Mirka could have in total; specify all possibilities.

(E. Novotná)

Hint. Which segment from the end is the first to have both legs dressed?
Let's outline the last few segments of the millipede Mirka (from left to right), with the top row representing the left legs and the bottom row the right legs. We mark the legs that are dressed in black and the bare legs in white, continuing this pattern until both legs on a segment are dressed—after which the pattern repeats:

![](https://cdn.mathpix.com/cropped/2024_04_17_adee0188cf1f8dcaeca5g-3.jpg?height=202&width=646&top_left_y=493&top_left_x=702)

If the millipede had 15 segments, both legs would be bare on 8 segments. We continue further until we get 14 segments with both legs bare:

![](https://cdn.mathpix.com/cropped/2024_04_17_adee0188cf1f8dcaeca5g-3.jpg?height=205&width=1054&top_left_y=854&top_left_x=501)

We continue further if the number of segments with both legs bare is the same:

![](https://cdn.mathpix.com/cropped/2024_04_17_adee0188cf1f8dcaeca5g-3.jpg?height=208&width=1096&top_left_y=1164&top_left_x=480)

The millipede Mirka had either 26 or 27 segments, which means either 52 or 54 legs.