Nth term of Van Eck Sequence
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