Assuming a potential as $$\Phi=\frac{1}{4\pi}\oint_S \frac{\vec M(\vec r')\cdot\hat n}{|\vec r - \vec r'|}da' \tag{1}\label{1}$$
where the integral can be written as
$$\int_V \nabla' \left(\frac{\vec M(\vec r')}{|\vec r - \vec r'|}dV'\right).$$
Then, if $M$ is constant
$$\Phi = \left(\frac{1}{4\pi} \int_{V} \frac{(\vec r - \vec r')}{|\vec r - \vec r'|^3}dV'\right) M$$
And the dipolar field can be taken as the gradient of that potential. Then
$$H_{d}=\sum_{i} \left[\frac{-1}{4\pi}\frac{\partial}{\partial_{i}n_{i}}\int_{V} \frac{(\vec r - \vec r')}{|\vec r - \vec r'|^3}dV'\right]$$ where $n_i$ is the scalar factor.
My question is where does this factor came from and why.