#JavaScript, <s>93</s> 91 bytes

Throws an overflow error for `0` or `1`, outputs `0` for `2`.

    n=>(g=x=>n-i?a[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(a[c=x]=++i,x>n)+g(2):0)(a=[i=c=2])

[Try it online](https://tio.run/##Dc1LDoIwEADQq7jQZCatjcEVxCnxDC6R2En5WKMtAWIK6tmRd4H34DcPtnfduPehqpeGFk8aWoqk/d7lXAgRyy/cCSY5I@k5v8Msp92M2YQQpcVTkrcQMQMuLMWShHAyao@ihQSzAwJT4chSUuJigx/Cs1bP0EKhlDr3PU@QpliqF3cANxnXw2w/UVDyM6rj6jJyP8IRhdmQ3hjRrBeqR3DeXL3B5Q8)

    n=>                                 :Anonymous function taking the input as an argument via parameter n
    (g=x=>                              :Main function g which takes the number we want to test as an argument via parameter x
      n-i?                              :If n is greater than the iteration counter i then
        a[++x]                          :Increment x and check if the array a has an element at that index
                                        :(On the first iteration x will be the singleton array [2] which gets coerced to a number before being incremented)
        |                               :Or
        (h=(y,z)=>z?h(z,y%z):y)(x,c)    :Helper function which returns the GCD of x and c, the last number in he sequence so far
        <2                              :If that's less than 2 then c & x are co-prime
        ?                               :If either of those checks are truthy then
          g(x)                          :Call g again with the incremented value of x
        :(                              :Else
          a[c=x]=++i,                   :Assign x to c for the next iteration, increment the iteration counter and drop that value into the array at index x
          x>n                           :Test if x is greater than the original input
          )+g(2)                        :Add the result of running g again, with an initial value of 2
      :0                                :Else if n==i then return 0, add that to the final value and return the result
    )(a=[i=c=2])                        :Immediately call g with an initial value of [2] which also gets assigned to a and initiate i & c with a value of 2