Let $p$ be an odd prime number and consider the set of $p-2$ integers that is $\mathbb{Z}\_p$ minus 0 and 1. Next define two bijective functions on this set
\begin{align}
f(x) &= 1-x \mod p
\end{align}
and
\begin{align}
g(x) &= x^{-1} \mod p \qquad \text{(the multiplicative inverse of $x$).}
\end{align}
One can view these two functions as group actions (generated by $f$ and $g$) on this set of integers and study the orbits. For example, if $p=7$, there are two orbits namely {2,4,6} and {3,5}.


For the general prime $p>3$, I can prove there is always an orbit consisting of $\{ 2,p-1, 2^{-1} \}$.


Question : I'm interested to find the number of orbits as a function of $p$.



Orbits in modular arithmetic
You can first notice that $fgfgfg(x)=x$ and conclude that most orbits have size $6$. It is easy to show that there is one orbit of size $3$ which you found and there is an orbit of size $2$ whenever there is a solution to $$x(1-x)\equiv 1\pmod{p}$$ this happens if $\binom{-3}{p}=1$ so if $p\equiv 1\pmod{3}$. You can easily show that there can't be orbits of size $4$ or $5$. So the answer is $\frac{p+1}{6}$ if $p\equiv -1\pmod{3}$ and $\frac{p+5}{6}$ if $p\equiv 1\pmod{3}$.


