# [Python 3], 83 bytes

<!-- language-all: lang-python -->

    f=lambda b,k=1,p=1:(k<b)|p%k|math.gcd(b**3-b,k)-1and f(b,k+1,p*k*k)or k
    import math

[Try it online!][TIO-kg8nfjvm]

-----

Uses [xnor's corollary to Wilson's theorem](https://codegolf.stackexchange.com/a/27022/48931). I still find it unbelievable that the shortest way to test for coprimality is `import math;math.gcd(x,y)<2`, but I couldn't find a shorter way.

[Python 3]: https://docs.python.org/3/
[TIO-kg8nfjvm]: https://tio.run/##FY3dDoMgDIWv51P0ZgkwXET2o2Y@ybILmHGSDiSGmyW@OysXX3vanpPGX1rWoHOex6/xdjJgJY5KxlENDB@W7/GIuzdpOX/eE7NC6JocvFYmTDAz0idyCxTI1w2wcj6uW4KSyDNtLLgAz1bCVcJNgmoImtq7BE1dXwjSXaEj@nJXpWhyNf1rqA5xcyExesZ5/gM "Python 3 – Try It Online"