I have used the attached photo to show that light takes the path described by snells law such that the time it takes from point A to B is minimized. But conceptually there is something wrong for me in this derivation. Because why would the light ray coming in, know that there is a different medium later on? It certainly would suggest that it does in this derivation, because here we vary the length that the light travels horizontally in the first medium with the idea of minimizing its path OVERALL. Am I misunderstanding something?