Nth term of Van Eck Sequence Output the Nth term of the Van Eck Sequence.

Van Eck Sequence is defined as:


Starts with 0. 
If the last term is the first occurrence of that term the next term is 0. 
If the last term has occurred previously the next term is how many steps back was the most recent occurrence.


https://oeis.org/A181391

https://www.youtube.com/watch?v=etMJxB-igrc

https://www.youtube.com/watch?v=8VrnqRU7BVU

Sequence: 
0,0,1,0,2,0,2,2,1,6,0,5,0,2,...

Tests:

Input | Output


1  | 0 
8  | 2 
19 | 5 
27 | 9 
52 | 42
64 | 0


EDIT

1 indexed is preferred, 0 indexed is acceptable; that might change some of the already submitted solutions.

Just the Nth term please.

Same (except for the seeing it already posted part), it seems code golfers and numberphile watchers have a decent overlap.
JavaScript (ES6),  46 41  37 bytes

n=&gt;(g=p=&gt;--n?g(g[p]-n|0,g[p]=n):p)(0)

Try it online!
How?
We don't need to store the full sequence. We only need to keep track of the last position of each integer that appears in the sequence. We use the underlying object of the recursive function \$g\$ for that purpose.
For a given term \$p\$, we don't need either to set \$g[p]\$ to its actual absolute position in the sequence because we're only interested in the distance with the current position. That's why we can just store the current value of the input \$n\$, which is used as a decrementing counter in the code.
Therefore, the distance is given by \$g[p]-n\$. Conveniently, this evaluates to NaN if this is the first occurrence of \$p\$, which can be easily turned into the expected \$0\$.
Commented
n =&gt; (             // n = input
  g = p =&gt;         // g = recursive function taking p = previous term of the sequence
                   //     g is also used as an object to store the last position of
                   //     each integer found in the sequence
    --n ?          // decrement n; if it's not equal to 0:
      g(           //   do a recursive call:
        g[p] - n   //     subtract n from the last position of p
                   //     if g[p] is undefined, the above expression evaluates to NaN
        | 0,       //     in which case we coerce it to 0 instead
        g[p] = n   //     update g[p] to n
      )            //   end of recursive call
    :              // else:
      p            //   we've reached the requested term: stop recursion and return it
)(0)               // initial call to g with p = 0