Bamidbar20:1

וַיָּבֹאוּ בְנֵי־יִשְׂרָאֵל כָּל־הָעֵדָה מִדְבַּר־צִן בַּחֹדֶשׁ הָרִאשׁוֹן וַיֵּשֶׁב הָעָם בְּקָדֵשׁ וַתָּמָת שָׁם מִרְיָם וַתִּקָּבֵר שָׁם׃

And the children of Israel, the whole congregation, came into the wilderness of Tzin in the first month, and the people stayed in Kadesh. And Miryam (Miriam) died there and was buried there.

Bamidbar 20:1

וַיָּבֹאוּ בְנֵי־יִשְׂרָאֵל כָּל־הָעֵדָה

Bamidbar / Numbers 20:1 · Pshat

Bamidbar 20:1

וַיָּבֹאוּ בְנֵי־יִשְׂרָאֵל כָּל־הָעֵדָה מִדְבַּר־צִן בַּחֹדֶשׁ הָרִאשׁוֹן וַיֵּשֶׁב הָעָם בְּקָדֵשׁ וַתָּמָת שָׁם מִרְיָם וַתִּקָּבֵר שָׁם׃

And the children of Israel, the whole congregation, came into the wilderness of Tzin in the first month, and the people stayed in Kadesh. And Miryam (Miriam) died there and was buried there.

Pshat פְּשָׁט

The fortieth year

Between two events a book may have one line, and life many years. How can we tell how much time passed between the red cow and Miryam's death?

Rashi: "the whole congregation" is the new generation: those destined to die in the wilderness had already died. Miryam's (Miriam's) death stands next to the red cow because the death of the righteous atones, like offerings. Rabbeinu Bahya: this is the fortieth year. Chizkuni: they stayed in Kadesh four months, until Aharon's (Aaron's) death in the month of Av.

How Rashi, Rabbeinu Bahya, and Chizkuni explain it

Rashi The whole congregation

The congregation complete: those destined to die in the wilderness had already died, and these were set apart for life.

Rashi And Miryam died there

Why is the account of Miryam's (Miriam's) death placed next to the law of the red cow? To tell you: as offerings atone, so the death of the righteous atones.

Rabbeinu Bahya The whole congregation, into the wilderness of Tzin

The congregation complete: the generation of the wilderness had already died, and now the sons came to the land of Kenaan (Canaan) in the first month, and it was the fortieth year. And know: in the Torah there are no events or prophecies at all except in the first year and the fortieth.

Chizkuni And the people stayed in Kadesh

They stayed there four months, until the month of Av, when they came to Mount Hor, and there Aharon (Aaron) died. And this is not the Kadesh of which it is said: "and you stayed in Kadesh many days." That Kadesh is Eil Paran, called Kadesh Barnea, and from there the scouts were sent. But this Kadesh, of which it is said "and the people stayed in Kadesh," is in the wilderness of Tzin, on the border of the land of Edom.

Takeaway

Almost forty years passed between the law of the red cow and Miryam's (Miriam's) death: the cow was burned in the second year after the exodus from Egypt (Chizkuni on Bamidbar / Numbers 19:2), and Miryam died in the fortieth.
The generation that left Egypt was destined to die in the wilderness after the story of the scouts (Bamidbar / Numbers 14:29).
"And you stayed in Kadesh many days" are Moshe's (Moses') words in the book of Devarim (Devarim / Deuteronomy 1:46). Aharon (Aaron) died on Mount Hor on the first day of the month of Av (Bamidbar / Numbers 33:38).