This is Problem A-6 on the 61st William Lowell Putnam Competition (2000); you'll find a complete set of answers to the 2000 Putnam here. (In fact solutions are available for the years 1995-2011.)
Kiran S. Kedlaya, Bjorn Poonen, Ravi Vakil, The William Lowell Putnam Mathematical Competition 1985-2000 gives another solution as well, which I adapt slightly.
Choose $m\ge 1$ minimal such that $a_m=0$. If $m=1$, we're done, so assume that $m>1$. An easy induction shows that $x_n=x_{n\,\bmod\,m}$ for all $n\ge 0$. Suppose that $0\le i; then $P^{m-j}(x_i)=P^{m-j}(x_j)=x_m=0\;,$ contradicting the minimality of $m$, so the numbers $x_0,\dots,x_{m-1}$ are distinct, and in general we have $x_j=x_k$ iff $j\equiv k\pmod m$. (Here $P^n=\underbrace{P\circ P\circ\ldots\circ P}_n$.)
Now $x_j=\min\{x_0,\dots,x_{m-1}\}$ and $x_k=\max\{x_0,\dots,x_{m-1}\}$. Then $x_k-x_j\mid P(x_k)-P(x_j)=x_{k+1}-x_{j+1}\ne 0\;,$ since $j+1\not\equiv k+1\pmod m$. On the other hand, $x_k-x_j\ge|x_{k+1}-x_{j+1}|$ by the choice of $j$ and $k$, so $x_k-x_j=|x_{k+1}-x_{j+1}|$, and $\{x_j,x_k\}=\{x_{j+1},x_{k+1}\}$. But $x_{j+1}\ne x_j$, since $m>1$, so $x_{j+1}=x_k$, and hence $x_{j+2}=x_{k+1}=x_j$. But then $j+2\equiv j\pmod m$, so $m\mid 2$, and therefore $m=2$, as desired.