If $R$ is a commutative ring with $1\ne 0$ and $R^m \cong R^n$ as direct sums of $R$-modules, then $m=n$.
There is a hint to this problem (I don't know how to use it): Reduce this to the case of a field.
I noticed that there was a similar question. But as a beginner of commutative algebra, I don't know if my question is closely related to that one. Thanks in advance!