#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
 
int prime[100000+310];
void makep(ll n){prime[0]=prime[1]=1;for(int i=2;i*i<=n;i++)if(!prime[i])for(int j=i*i;j<=n;j+=i)prime[j]=1;}
 
int main(){
	makep(100300);
	ll n,p;
	while(scanf("%lld%lld",&n,&p),~n){
		n++;
		ll crr=0;
		while(p>0){
			rep(i,0,crr/2+1)if(!prime[n+i]&&!prime[n+crr-i])p--;
			if(p>0)crr++;
		}
		printf("%lld\n",n+n+crr);
	}
}
