One of the most beautiful legends of Sant Joan night, and in which the devil is involved, is the one that tells us that one year, on that same night, the devil ran through the region of La Cerdanya, looking the treasures and souls that belonged to him and that he had scattered all over the world. As he ran he lost the hat he was wearing, which was what he needed to remember where his treasures and souls were. No matter how much he looked for it, he did not find it, which is why it is said that if someone finds it and puts it on, he will discover all the places where the devil had hidden his treasures. At the same time, it must also be said that until he finds his hat, the devil will not be able to take any soul to hell, since, without his hat, he cannot recognize which ones belong to him.
So the hat needs to be found before the Devil.