4. Find all positive integers $n$, such that
$$
\frac{n^{3 n-2}-3 n+1}{3 n-2} \in \mathbf{Z} \text {. }
$$
4. Since $(3 n-2) \mid \left(n^{3 n-2}-3 n+1\right)$, therefore,
$$
\begin{array}{l}
(3 n-2) \mid\left(n^{3 n-2}-1\right) \\
\Rightarrow n^{3 n-2} \equiv 1(\bmod 3 n-2) .
\end{array}
$$

Notice that $n$ must be odd (otherwise, the even number $3 n-2$ could divide the odd number $n^{3 n-2}-3 n+1$, which is a contradiction).
Let $p>2$ be the smallest prime factor of the odd number $3 n-2$.
Clearly, $p \nmid n$.
Then by Fermat's Little Theorem,
$$
n^{p-1} \equiv 1(\bmod p) \text {. }
$$

Thus, $n^{3 n-2} \equiv 1(\bmod p)$.
Let $r$ be the order of $n$ modulo $p$. Then
$$
r \mid (p-1), r \mid (3 n-2) \text {. }
$$

Since $r \mid (p-1, 3 n-2)$ and $(p-1, 3 n-2) = 1$, we get $r=1 \Rightarrow n \equiv 1(\bmod p)$.
From $p \mid (n-1)$ and $p \mid (3 n-2)$, we get $p \mid 1$, which is a contradiction.
Therefore, $3 n-2=1$, i.e., $n=1$ is the only solution.