Infinite dimensional Morse theory and Fermat’s principle in general relativity. I