This was one of the questions in a Olympiad number theory course. I used brute force with the CRT: x is odd, not divisible by 3 and its square leaves remainder 1 mod 5 (therefore $x \equiv \pm 1 \pmod 5$). So $x \equiv \pm 1 \pmod{30}$ or $x \equiv \pm 11 \pmod{30}$ . As it can be noted, this is extremely specific and, therefore, a little bit ugly.
How do I generalize this problem for an arbitrary $n$? For example, taking $n = 24$, we have that $5^2 \equiv 1 \pmod n$. So there must be a nontrivial pattern. How do I generalize it?
HINT: write $$(x-1)(x+1)\equiv 0 \mod 30$$