5. Find all surjective functions $f: \mathbf{N}_{+} \rightarrow \mathbf{N}_{+}$ such that for any $m, n \in \mathbf{N}_{+}$ and any prime $p$, $f(m+n)$ is divisible by $p$ if and only if $f(m) + f(n)$ is divisible by $p$.
5. $f(n)=n$.

Assume the function $f: \mathbf{N}_{+} \rightarrow \mathbf{N}_{+}$ satisfies the condition.
Lemma For any prime $p$ and any $x, y \in \mathbf{N}_{+}, x \equiv y(\bmod p)$ if and only if $f(x) \equiv f(y)(\bmod p)$. Additionally, $p \mid f(x)$ if and only if $p \mid x$.

Proof of the lemma: For any prime $p$, since $f$ is surjective, there exists $x \in \mathbf{N}_{+}$ such that $p \mid f(x)$.

Let $d=\min \left\{x \in \mathbf{N}_{+}|p| f(x)\right\}$. Then for any $k \in \mathbf{N}_{+}$, we have $p \mid f(k d)$.
In fact, we can use mathematical induction on $k$.
When $k=1$, it is obviously true.
If $p \mid f(k d)$, combined with $p \mid f(d)$, by the known condition we get $p \mid f(k d+d)$, i.e.,
$p \mid f((k+1) d)$.
Assume there exists an $x \in \mathbf{N}_{+}$ such that $d \nmid x$, but $p \mid f(x)$.
Let $y=\min \left\{x \in \mathbf{N}_{+}|d \nmid x, p \mid f(x)\right\}$.
By the definition of $d$, we know $y>d$. Hence $d \nmid (y-d)$.
Since $p|f(d), p| f(d+(y-d))=f(y)$, by the known condition we get $p \mid[f(d)+f(y-d)]$.
Thus, $p \mid f(y-d)$, which contradicts the definition of $y$.
Therefore, there does not exist such an $x$, and we have
$p|f(x) \Leftrightarrow d| x$.
For any $x, y \in \mathbf{N}_{+}$, and $x \equiv y(\bmod d)$, since $p \mid f(x+(2 x d-x))=f(2 x d)$, and because $d \mid[2 x d+(y-x)]=y+(2 x d-x)$, we have
$p \mid f(y+(2 x d-x))$.
By the known condition we get
$p \mid[f(x)+f(2 x d-x)]$,
$p \mid[f(y)+f(2 x d-x)]$.
Thus, $f(x) \equiv-f(2 x d-x) \equiv f(y)(\bmod p)$.
On the other hand, if $f(x) \equiv f(y)(\bmod p)$, and by $p \mid(f(x)+f(2 x d-x))$ we get
$$
p \mid(f(x)+f(2 x d-x)+f(y)-f(x)),
$$

i.e., $p \mid[f(y)+f(2 x d-x)]$.
Thus, by the known condition we get
$p \mid f(y+(2 x d-x))$.
By (1) we get
$0 \equiv y+(2 x d-x) \equiv(y-x)(\bmod d)$.
Therefore, we have proved
$x \equiv y(\bmod d) \Leftrightarrow f(x) \equiv f(y)(\bmod p)$. (2)
Since $1,2, \cdots, d$ are different residues modulo $d$, by (2) we know $f(1), f(2), \cdots, f(d)$ are different residues modulo $p$, i.e., there are at least $d$ different residues. Hence $p \geqslant d$.

On the other hand, since $f$ is surjective, there exist $x_{1}, x_{2}, \cdots, x_{p} \in \mathbf{N}_{+}$ such that $f\left(x_{i}\right)=i(i=1,2, \cdots, p)$. By (2) we know $x_{1}, x_{2}, \cdots, x_{p}$ are different residues modulo $d$, and for the same reason we get $d \geqslant p$.
Therefore, $d=p$.
Thus, (1) and (2) are the conclusions of the lemma.
Returning to the original problem.
We prove by induction on $n$: $f(n)=n$.
When $n=1$, by the lemma, for any prime $p, p \nmid f(1)$. Thus, $f(1)=1$.
When $n>1$, assume for $n-1$ we have
$f(n-1)=n-1$.
Let $k=f(n)$. Then there exists a prime $q$ such that $q \mid n$.
By the lemma we know $q \mid f(n)=k$. Thus, $k>1$.
If $k>n$, then $k-n+1>1$, and there exists a prime $p$ such that $p \mid(k-n+1)$, i.e.,
$k \equiv(n-1)(\bmod p)$.
By the induction hypothesis
$f(n-1)=n-1 \equiv k=f(n)(\bmod p)$.
By the lemma we get $n-1 \equiv n(\bmod p)$, which is a contradiction.
If $k<n$, then $n-k+1>1$, so there exists a prime $p$ such that $p \mid(n-k+1)$, i.e., $n \equiv k-1(\bmod p)$.
Again using the lemma we get
$$
k=f(n) \equiv f(k-1)=k-1(\bmod p),
$$

which is a contradiction.
Therefore, $k=n$, i.e., $f(n)=n$.
It is easy to verify that $f(n)=n$ satisfies the condition.