We are given
$$f(x)=x^2+px+q$$
and we need to find the possible number of distinct real roots of
$$f(f(x))=x$$
Consider
$$f(f(x))-x=0$$
Now,
$$f(f(x))-x=f(f(x))-f(x)+f(x)-x$$
Also,
$$f(f(x))-f(x)=(f(x))^2+pf(x)+q-\big(x^2+px+q\big)$$
$$=(f(x))^2-x^2+p(f(x)-x)$$
$$=(f(x)-x)(f(x)+x)+p(f(x)-x)$$
$$=(f(x)-x)(f(x)+x+p)$$
Hence,
$$f(f(x))-x=(f(x)-x)(f(x)+x+p)+(f(x)-x)$$
$$=(f(x)-x)\big(f(x)+x+p+1\big)$$
Substituting
$$f(x)=x^2+px+q$$
gives
$$\big(x^2+(p-1)x+q\big)\big(x^2+(p+1)x+(p+q+1)\big)=0$$
Let
$$g(x)=x^2+(p-1)x+q$$
and
$$h(x)=x^2+(p+1)x+(p+q+1)$$
Their discriminants are
$$\Delta_1=(p-1)^2-4q$$
and
$$\Delta_2=(p+1)^2-4(p+q+1)$$
$$=p^2-2p-3-4q$$
Therefore,
$$\Delta_1-\Delta_2=4$$
or
$$\Delta_1=\Delta_2+4$$
Now consider the possible cases.
If
$$\Delta_2>0$$
then
$$\Delta_1>4>0$$
Both quadratics have two distinct real roots, so
$$N=4$$
If
$$\Delta_2=0$$
then
$$\Delta_1=4$$
The quadratics share a root and hence
$$N=2$$
If
$$-4<\Delta_2<0$$
then only
$$g(x)$$
has real roots, giving
$$N=2$$
If
$$\Delta_2=-4$$
then
$$\Delta_1=0$$
and
$$g(x)$$
has one repeated real root, so
$$N=1$$
If
$$\Delta_2<-4$$
then
$$\Delta_1<0$$
and neither quadratic has a real root, so
$$N=0$$
Hence the possible values of
$$N$$
are
$$0,1,2,4$$
Therefore
$$N=3$$
is impossible.
Final Answer :
$$3$$