#JavaScript, 28 bytes

<!-- language: lang-js -->

    n=>g=(x=2)=>++n%x?--x:g(++x)

---
##Test it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    o.innerText=(f=

    n=>g=(x=2)=>++n%x?--x:g(++x)

    )(i.value=2521)();oninput=_=>o.innerText=f(+i.value)()

<!-- language: lang-html -->

    <input id=i><pre id=o>

<!-- end snippet -->