I belive the answer is given by $$ d = h [\tan \theta_1 + \tan \theta_2]$$ each term giving you thee horizontal distance travelled for each medium.
Now, according to Snell's law $$\frac{\sin \theta_1}{\sin \theta_2} = \frac{n_1}{n_2}$$ so $$\theta_2 = \arcsin \left(\sin \theta_1 \cdot \frac{n2}{n1}\right)$$ and upon substituing $$ d = h \left[\tan \theta_1 + \tan \,\arcsin \left(\sin \theta_1 \cdot \frac{n2}{n1}\right)\right] $$