IMO 2018

PROBLEM 2
 
Find all integers $n \geq 3$ for which there exist real numbers $a_1, a_2, \dots a_{n + 2}$ satisfying $a_{n + 1} = a_1$, $a_{n + 2} = a_2$ and

$$a_ia_{i + 1} + 1 = a_{i + 2}$$
For $i = 1, 2, \dots, n$.