5. Solution: Let Masha have $x$ rubles, and Petya have $y$ rubles, then

$n(x-3)=y+3$

$x+n=3(y-n)$

Express $x$ from the second equation and substitute into the first:

$n(3 y-4 n-3)=y+3$,

$3 n y-y=4 n^{2}+3 n+3$,

$y=\left(4 n^{2}+3 n+3\right):(3 n-1)$

For $y$ to be an integer, $\left(4 n^{2}+3 n+3\right)$ must be divisible by (3n-1)

$\left(4 n^{2}+3 n+3\right):(3 n-1)$

$3\left(4 n^{2}+3 n+3\right)-4 n(3 n-1):(3 n-1)$

$13 n+9:(3 n-1)$

$3(13 n+9)-13(3 n-1):(3 n-1)$

$40:(3 n-1)$

(3n-1) is a divisor of 40 only for $n=1 ; 2 ; 3 ; 7$

Let's check if $x$ and $y$ are natural numbers.

$n=1, y=5, x=11$

$n=2, y=5, x=7$

$n=3, y=6, x=6$

$n=7, y=11, x=5$