5. Given $a_{1}=1, a_{2}=3$,
$$
a_{n+2}=(n+3) a_{n+1}-(n+2) a_{n} \text {, }
$$

when $m \geqslant n$, $a_{m}$ is divisible by 9. Then the minimum value of $n$ is $\qquad$ .
5.5.

Notice,
$$
\begin{array}{l}
a_{n+2}-a_{n+1}=(n+2)\left(a_{n+1}-a_{n}\right) \\
=\cdots=(n+2)(n+1) n \cdots \cdots \cdot 3\left(a_{2}-a_{1}\right) \\
=(n+2)!.
\end{array}
$$

Thus, $a_{n}=a_{1}+\sum_{i=1}^{n-1}\left(a_{i+1}-a_{i}\right)=\sum_{i=1}^{n} i!$.
Given $a_{1}=1, a_{2}=3$, we have
$$
a_{3}=9, a_{4}=33, a_{5}=153 \text {, }
$$

At this point, 153 is divisible by 9.
When $m>5$, $a_{m}=a_{5}+\sum_{k=6}^{m} k!$, and for $k \geqslant 6$, $k!$ is divisible by 9, so when $m \geqslant 5$, $a_{m}$ is divisible by 9.
Therefore, the smallest value of $n$ is 5.