6. For each integer $n$ greater than or equal to 2, let $f(n)$ denote the number of distinct solutions to $\sin n x=\sin x$ in the interval $[0, \pi]$, and let $g(n)$ denote the number of distinct solutions to $\cos n x=\cos x$ in the interval $[0, \pi]$. Then $\sum_{n=2}^{2017}(g(n)-f(n))=$ $\qquad$
6.504

Analysis: From $\sin n x=\sin x$ we get: $n x=2 k \pi+x$ or $2 k \pi+\pi-x$, which means $x=\frac{2 k+1}{n+1} \pi$ or $x=\frac{2 k}{n-1} \pi$. Also, since $x \in[0, \pi]$, we have $0 \leq k \leq \frac{n}{2}$ or $0 \leq k \leq \frac{n-1}{2}$. However, the two sets of values may overlap. If $\frac{2 k+1}{n+1} \pi=\frac{2 m}{n-1} \pi$, it is found that: when $n=4 t+1, t \in N^{*}$, one set overlaps. Similarly, for $\cos n x=\cos x, x=\frac{2 k}{n-1} \pi$ or
$$
x=\frac{2 k}{n+1} \pi,
$$
$0 \leq k \leq \frac{n-1}{2}$ or $0 \leq k \leq \frac{n+1}{2}, n=2 t+1, t \in N^{*}$, one set overlaps. Comparing the values of the two solutions, we find that $0 \leq k \leq \frac{n-1}{2}$ is the common part. When $n$ is odd, $0 \leq k \leq \frac{n+1}{2}$ has one more solution than $0 \leq k \leq \frac{n}{2}$, but $g(n)$ repeats one set when $n=2 t+1, t \in N^{*}$. $f(n)$ only repeats one set when $n=4 t+1, t \in N^{*}$. In essence, only when $n=4 t+1, t \in N^{*}$, $g(n)$ has one more solution than $f(n)$, and in other cases, the solutions are the same. Therefore,
$$
\sum_{n=2}^{2017}(g(n)-f(n))=\frac{2017-5}{4}+1=504
$$