5. For the positive integer $n$, define $a_{n}$ as the unit digit of $n^{(n+1)^{n-1}}$. Then $\sum_{n=1}^{2018} a_{n}=$ $\qquad$ .
5. 5857 .

When $n \equiv 0,1,5,6(\bmod 10)$, $a_{n} \equiv n^{(n+1)^{n+2}} \equiv n(\bmod 10)$; when $n \equiv 2,4,8(\bmod 10)$, $(n+1)^{n+2} \equiv 1(\bmod 4)$, then $a_{n} \equiv n^{(n+1)^{n+2}}=n^{4 k+1} \equiv n(\bmod 10)$; when $n \equiv 3,7,9(\bmod 10)$, $(n+1)^{n+2} \equiv 0(\bmod 4)$, then $a_{n} \equiv n^{(n+1)^{n+2}}=n^{4 k} \equiv 1(\bmod 10)$. Thus, $a_{n}$ is a periodic sequence, with the smallest positive period being 10.
$$
\text { Hence } \sum_{n=1}^{2018} a_{n}=201 \times 29+28=5857 \text {. }
$$