#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
  const int NUM = 200000;
  bool prime[NUM];
  prime[0] = false;
  prime[1] = false;
  for(int i=2;i<NUM;i++)
    prime[i] = true;
  for(int i=2;i*i<NUM;i++)
    for(int k=2;i*k<NUM;k++)
      prime[i*k]=false;

  int n,p,pn;
  while(true){
    cin >> n >> p;
    if (n==-1)
      break;
    int sum[p*p];
    for(int i=0;i<p*p;i++)
      sum[i] = 0;
    pn = n+1;
    for(int i=0;i<p;i++){
      while(!prime[pn])
	pn++;
      for(int j=0;j<p;j++){
	sum[j*p+i] += pn;
	sum[i*p+j] += pn;
      }
      pn++;
    }
    for(int i = 0;i<p*p;i++){
      if(i/p>i%p)
	sum[i] = 1000000;
    }
    
    sort(sum,sum+p*p);
    cout << sum[p-1] << endl;
  }
  return 0;
}