Determine all $n \geq 3$ for which there exist $n$ positive integers $a_1, a_2, \ldots, a_n$ such that any two of them have a common divisor greater than 1, but any three of them are coprime. Furthermore, assuming that the numbers $a_i$ are less than 5000, find the greatest possible $n$.

Let's think step by step and output the final answer within \boxed{}.
user