5. Let $S=\{1,2,3,4\}$, and the sequence $a_{1}, a_{2}, \cdots, a_{n}$ has the following property: for any non-empty subset $B$ of $S$, there are consecutive $|B|$ terms in the sequence that exactly form the set $B$. Find the minimum value of $n$.
(1997, Shanghai High School Mathematics Competition)