6. A box contains $m$ red balls, 9 white balls, and $n$ black balls, all of which are identical except for their color. If at least 17 balls must be drawn to ensure that there are 5 red balls, and at least 17 balls must be drawn to ensure that there are 8 balls of the same color. Then the value of the algebraic expression $|m-n|+|m-5|$ is $(\quad)$.
(A) 7
(B) 6
(C) 5
(D) 4
6. D.

From the problem, we know that $5+9+n=17$.
Solving for $n$ gives $n=3$.
If $m \geqslant 8$, then to draw eight balls of the same color, the minimum number of balls to be drawn is
$$
(8-1)+(8-1)+n+1=18>17,
$$

which is a contradiction.
Therefore, $m<8$.

Thus, $m+8+n=17 \Rightarrow m=6$.
Hence, $|m-n|+|m-5|=4$.