C (gcc), 87 8686 84 bytes
Saved a byte thanks to the man himself Arnauld!!!
i;q;p;m;f(b){for(p=m=b;p|!q;++m)for(q=p=i=1;++i<m;p=p&&m%iq=p=i=1;++i<m;)q=m%iq=m%i<1?q:p=0,q&&b*~-b*~b%i;b*~b%i:q;--m;}
Port of my Python answer.