6. Given the set $M=\{1,2, \cdots, k\}$, for $A \subseteq M$, denote the sum of all elements in $A$ as $S(A)$. If $M$ can be divided into two disjoint subsets $A$ and $B$, and $A \cup B=M, S(A)=2 S(B)$. Find all values of $k$.
(1994, Sichuan Province High School Mathematics Competition)
(Tip: Since $A \cup B=M, A \cap B=\varnothing, S(A)=$ $2 S(B)$, thus $S(M)=3 S(B)=\frac{k(k+1)}{2}$ is a multiple of 3, i.e., $3 \mid k$ or $3 \mid(k+1)$. (1) When $k=3 m$, $A=\{1,3,4,6$, $\cdots, 3 m-2,3 m\}, B=\{2,5,8, \cdots, 3 m-1\}$ meets the requirements;
(2) When $k=3 m-1$, $A=\{2,3,5,6,8, \cdots, 3 m-3,3 m$ $-1\}, B=\{1,4,7, \cdots, 3 m-2\}$ meets the requirements. Therefore, $k=$ $3 m$ or $k=3 m-1$. )