#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
 
int prime[100000+510];
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(100500);
	ll n,p;
	while(scanf("%lld%lld",&n,&p),~n){
		n++;
		ll a[510]={};
		rep(i,0,500)if(!prime[n+i])rep(j,i,500-i)if(!prime[n+j])a[i+j]++;
		ll crr=0;
		rep(i,0,p)while(!a[crr]--)crr++;
		printf("%lld\n",n+n+crr);
	}
}
