This is a standard statistical inference problem involving the classical occupancy distribution (see e.g., O'Neill 2019). Since $R$ is the number of repeated balls, the number of distinct balls selected in the sample is given by:
$$K = N-R \ \sim \ \text{Occ}(N, M).$$
The probability mass function for this random variable is:
$$p(K=k|N,M) = \frac{(N)_k \cdot S(M,k)}{N^M} \cdot \mathbb{I}(1 \leqslant k \leqslant \min(M,N)),$$
where the values $S(M,k)$ are the Stirling numbers of the second kind and $(N)_k$ are the falling factorials. The classical occupancy distribution has been subject to a great deal of analysis in the statistical literature, including analysis of statistical inference for the size parameter $N$ (see e.g., Harris 1968). The form of this distribution and its moments is known, so deriving the MLE or MOM estimators is a relatively simple task.
Maximum-likelihood estimator (MLE): Since the size parameter is an integer, we can find the MLE using discrete calculus. For any value $1 \leqslant k \leqslant \min(M,N)$ the forward difference of the probability mass function with respect to $N$ can be written as:
$$\begin{align}
\Delta_N p(k)
&\equiv p(K=k|N+1,M) - p(K=k|N,M) \\[10pt]
&= \frac{(N+1)_k \cdot S(M,k)}{(N+1)^M} - \frac{(N)_k \cdot S(M,k)}{N^M} \\[6pt]
&= S(M,k) \bigg[ \frac{(N+1)_k}{(N+1)^M} - \frac{(N)_k}{N^M} \bigg] \\[6pt]
&= S(M,k) \cdot \frac{(N)_{k}}{(N+1)^M} \bigg[ \frac{N+1}{N-k+1} - \Big( \frac{N+1}{N} \Big)^M \ \bigg] \\[6pt]
\end{align}$$
Thus, if we observe $K=k$ then the maximum-likelihood-estimator (MLE) is given by:
$$\hat{N}_\text{MLE} = \max \bigg \{ N \in \mathbb{N} \ \Bigg| \ \frac{N+1}{N-k+1} < \Big( \frac{N+1}{N} \Big)^M \bigg \}.$$
(There may be cases where the MLE is not unique, since we can also use the $\leqslant$ instead of $<$ in the inequality in this equation.) Here is a simple function in R
to compute the MLE and an example when the input values are fairly large.
MLE.Occ.n <- function(m, k) {
n <- k
while ((n+1)/(n-k+1) >= (1+1/n)^m) { n <- n+1 }
n }
MLE.Occ.n(m = 1000, k = 649)
[1] 1066
Estimation using method-of-moments: The first four moments of the classical occupancy distribution are given in O'Neill (2019) (Section 2). The expected number of different balls is:
$$\mathbb{E}(K) = N \Bigg[ 1 - \Big( 1-\frac{1}{N} \Big)^M \Bigg].$$
Thus, if we observe $K=k$ then the method-of-moments estimator will approximately solve the implicit equation:
$$\log \hat{N}_\text{MOM}^* - \log k + \text{log1mexp} \Bigg[ - M \log \Big( 1-\frac{1}{\hat{N}_\text{MOM}^*} \Big) \Bigg] = 0.$$
You can solve this equation numerically to obtain a real value $\hat{N}_\text{MOM}^*$ and then use one of the two surrounding integers as $\hat{N}_\text{MOM}$ (these each give slight over- and under-estimates for the true expected value and you can then pick between these using some appropriate method --- e.g., rounding to the nearest integer). Here is a function in R
to compute the method-of-moment estimator. As can be seen, it gives the same result as the MLE in the present example.
MOM.Occ.n <- function(m, k) {
FF <- function(n) { log(n) - log(k) + VGAM::log1mexp(-m*log(1-1/n)) }
UPPER <- m*k/(m-k)
n.real <- uniroot(f = FF, lower = k, upper = UPPER)$root
round(n.real, 0) }
MOM.Occ.n(m = 1000, k = 649)
[1] 1066