Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		n, p := io.NextInt(), io.NextInt()
		if n == -1 {
			break
		}
		isPrime := SieveOpt(200000)
		prime := make([]int, p)
		cnt := 0
		if n < 2 {
			prime[0] = 2
			cnt++
		}
		for i := (n + 1) / 2; i < len(isPrime) && cnt < p; i++ {
			if isPrime[i] == true {
				prime[cnt] = 2*i + 1
				cnt++
			}
		}
		comb := make([]int, 0, p*p)
		for i := 0; i < p; i++ {
			for j := i; j < p; j++ {
				comb = append(comb, prime[i]+prime[j])
			}
		}
		sort.Ints(comb)
		io.Println(comb[p-1])
	}
}

func SieveOpt(x int) []bool {
	flags := make([]bool, x/2) // flags[i] := 2*j+1 is prime
	for i := 1; i < x/2; i++ {
		flags[i] = true
	}
	sqrtXi := int(math.Ceil(math.Sqrt(float64(x)))) / 2
	for i := 1; i < sqrtXi; i ++ {
		if !flags[i] {
			continue
		}
		for j := 2 * i * (i + 1); j < x/2; j += 2*i + 1 {
			flags[j] = false
		}
	}
	return flags
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 101198
int prime[MAX];
int p[11000];

int main(){
  int i,j,k,l,m;
  prime[0]=-1;prime[1]=-1;
  for(k=0,i=2;i<MAX;i++)
    if(!prime[i])
      for(prime[i]=k,p[k++]=i,j=i*2;j<MAX;j+=i)
        prime[j]=-1;
  for(;scanf("%d%d",&l,&m),~l;){
    vector<int> v;
    for(l++;prime[l]<0;l++);
    for(i=prime[l];i<prime[l]+m;i++)
      for(j=i;j<prime[l]+m;j++)
        v.push_back(p[i]+p[j]);
    sort(v.begin(),v.end());
    printf("%d\n",v[m-1]);
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int n,p;
  vector<int> prm;
  bool isP[200000];

  for( int i=0;i<200000;i++ )
    isP[i]=true;

  int ff=-1;
  for( int i=2; i<200000;i+=2 ){
    if( isP[i] ){
      prm.push_back( i );
      for( int j=i*2;j<200000;j+=i )
	isP[j] = false;
      if( i==2 ) i--;
      if( ff<0 && i>100000 )
	ff=prm.size();
      if( ff>0 && prm.size() > ff + 200 )
	break;
    }
  }

  while( cin>>n>>p && (n!=-1 && p!=-1) ){
    int a[100];
    vector<int> v;

    int index=0;
    while( prm[index]<=n ) index++;
    for( int i=0;i<100;i++ )
      a[i] = prm[index++];

    for( int i=0;i<100;i++ ){
      for( int j=i;j<100;j++ )
	v.push_back( a[i]+a[j] );
    }
    sort( v.begin(),v.end() );
    /*
    for( int i=0;i<100;i++ )
      cout << " " << v[i];
    cout << endl;
    */

    cout << v[p-1] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

#define MAX 1000001
bool p[MAX];

int main(){
  memset(p, true, sizeof(p));
  p[0] = p[1] = false;
  for(int i = 0 ; i*i < MAX ; i++){
    if(p[i]){
      for(int j = 2*i ; j < MAX ; j += i){
	p[j] = false;
      }
    }
  }

  int N, P;
  while(cin >> N >> P){
    if(N == -1 && P == -1) break;
    vector<int> prime;
    
    for(int i = N+1 ; prime.size() < 100 ; i++){
      if(p[i]) prime.push_back(i);
    }
    vector<int> C;
    for(int a = 0 ; a < prime.size() ; a++){
      for(int b = a ; b < prime.size() ; b++){
	C.push_back(prime[a]+prime[b]);
      }
    }
    sort(C.begin(), C.end());
    
    //vector<int>::iterator it = unique(C.begin(), C.end());
    //C.erase(it, C.end());
    cout << C[P-1] << endl;
   
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

bool IsPrime[1000000];
vector<int> Prime;
void PrimeCalc()
{
	memset(IsPrime, 1, sizeof(IsPrime));
	IsPrime[0] = IsPrime[1] = false;
	for(int i = 2; i < 1000000; ++i)
	{
		if(IsPrime[i])
		{
			Prime.push_back(i);
			for(int j = 2 * i; j < 1000000; j += i)
			{
				IsPrime[j] = false;
			}
		}
	}
}

void solve()
{
	PrimeCalc();
	int N, P;
	while(cin >> N >> P)
	{
		if(N == -1 && P == -1)
		{
			break;
		}
		int pos = 0;
		while(Prime[pos] <= N)
		{
			++pos;
		}
		vector<int> Vec;
		for(int i = 0; i < 100; ++i)
		{
			for(int j = i; j < 100; ++j)
			{
				Vec.push_back(Prime[pos + i] + Prime[pos + j]);
			}
		}
		sort(Vec.begin(), Vec.end());
		cout << Vec[P - 1] << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<functional>
#include<climits>
using namespace std;
typedef pair<int,int>pii;
const int INF=1e9;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};


int main(){
    bool f[1000000];fill_n(f,1000000,true);
    f[0]=f[1]=false;
    vector<int>prime;
    for(int i=0;i<1000000;i++){
        if(!f[i])continue;
        for(int j=i+i;j<1000000;j+=i)f[j]=false;
        prime.push_back(i);
    }

    int n,p;
    while(cin>>n>>p,n!=-1||p!=-1){
        int pos=upper_bound(prime.begin(),prime.end(),n)-prime.begin();
        vector<int>V;
        for(int i=pos;i<=pos+50;i++){
            for(int j=i;j<=pos+50;j++){
                V.push_back(prime[i]+prime[j]);
            }
        }
        sort(V.begin(),V.end());
        cout<<V[p-1]<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const int N = 150000;

int main() {
    int n, p;
    VI primes, d(N,1);
    FOR(i,2,N-1){
        if (!d[i]) continue;
        primes.push_back(i);
        int x = 2*i;
        while (x < N){
            d[x] = 0;
            x += i;
        }
    }
    while (cin >> n >> p && p >= 0){
        VI cand;
        int i = 0;
        while (primes[i] <= n) i++;
        FOR(j,i,i+p) FOR(k,i,j){
            cand.push_back(primes[j]+primes[k]);
        }
        sort(cand.begin(), cand.end());
        cout << cand[p-1] << endl;
    }
    return 0;
}