5. Given a periodic sequence $\left\{x_{n}\right\}$ satisfying $x_{n}=\left|x_{n-1}-x_{n-2}\right|(n$ $\geqslant 3$ ). If $x_{1}=1, x_{2}=a \geqslant 0$, then when the period of the sequence is the smallest, the sum of the first 2002 terms of the sequence is ( ).
(A) 2002
(B) 1335
(C) 1949
(D) 1428
5.(B).

Let the sequence $\left\{x_{n}\right\}$ have a period of $T, T \neq 1, T \geqslant 2$.
If $T=2$, then $x_{3}=|a-1|=1$. This gives $a=0$ or 2.
From $x_{4}=\left|x_{3}-x_{2}\right|=|1-a|=a$, we get $a=\frac{1}{2}$. This contradicts $a=0$ or 2. Therefore, $T \geqslant 3$.
When $T=3$, from $x_{4}=|| a-1|-a|=1$, we get $a \geqslant 1$ or $a=0$.
From $x_{5}=|1-| a-1||=a$, we get $0 \leqslant a \leqslant 1$.
Thus, $a=0$ or 1.
In this case, the sequence $\left\{x_{n}\right\}$ is either $1,0,1,1,0,1, \cdots$, or 1,1, $0,1,1,0, \cdots$. Therefore, $S_{2002}=2 \times 667+1=1335$.