By Fermat's little theorem we know that
$a^{p-1} \equiv 1 \pmod{p}$ for all primes p.
But it is often possible to find $x$ such that $a^{x} \equiv 1 \pmod{p}$ and x < p - 1. Is there anyway to predict when such an $x$ exists or what it is? I wrote a program to generate the minimal such $x$ for all $a$ less than a prime $p$, but I can't figure out any pattern.