New Order #1: How does this feel? Introduction

Putting all positive numbers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive numbers.

The first challenge in this series is to output a(n) for a given n as input, where a(n) is A064413, also known as the EKG sequence because the graph of its values resembles an electrocardiogram (hence the "How does this feel" reference). Interesting properties of this sequence are that all positive integers appear exactly once. Another notable feature is that all primes occur in increasing order.



Task

Given an integer input n, output a(n).

\$a(n)\$ is defined as:


\$a(1) = 1; a(2) = 2;\$
for \$n &gt; 2\$, \$a(n)\$ is the smallest number not already used which shares a factor with \$a(n-1)\$


Note: 1-based indexing is assumed here; you may use 0-based indexing, so \$a(0) = 1; a(1) = 2\$, etc. Please mention this in your answer if you choose to use this.

Test cases

Input | Output
--------------
1     | 1
5     | 3
20    | 11
50    | 49
123   | 132
1234  | 1296
3000  | 3122
9999  | 10374


Rules


Input and output are integers (your program should at least support input and output in the range of 1 up to 32767)
Invalid input (floats, strings, negative values, etc.) may lead to unpredicted output, errors or (un)defined behaviour.
Default I/O rules apply.
Default loopholes are forbidden.
This is code-golf, so the shortest answers in bytes wins


Final note

See this related PP&amp;CG question.
Haskell, 66 65 64 bytes



f n|n&lt;3=n|m&lt;-n-1=[k|k&lt;-[1..],k`gcd`f m&gt;1,all(/=k)$f&lt;$&gt;[1..m]]!!0


Try it online!