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 <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
 
using namespace std;
  
const double eps=1e-10;
 
const int dy[]={-1,-1,0,1,1, 1, 0,-1};
const int dx[]={ 0, 1,1,1,0,-1,-1,-1};
 
int main()
{
 
	vector<long> prime;
	prime.push_back(2);
	prime.push_back(3);
	prime.push_back(5);
 
	for(int i=7;prime.back()<=100000+100*100;i+=2){
		bool ok=true;
		for(int j=0;prime[j]*prime[j]<=i && ok;j++){
			if(i%prime[j]==0) ok=false;
		}
		if(ok) prime.push_back(i);
//	  if(ok) cout<<i<<endl;
	}
 
	long n;
	int p;
	while(cin>>n>>p,n!=-1){
		vector<long> v;

		long pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
		for(long i=pos;i<pos+p;i++){
			for(long j=i;j<pos+p;j++) v.push_back(prime[i]+prime[j]);
		}

		sort(v.begin(),v.end());
//		for(int i=0;i<v.size();i++) cout<<i<<" "<<v[i]<<endl;
//		cout<<endl;

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

C++: #include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100000

int main(){
        int n, p, sosuu[100],count=0,sum[10000];
        string keymoji;

        cin >> n >> p;
        while(n != -1 && p != -1){
                int i=n;
                count = 0;
                int m=sqrt(n);
                if(i<2)i=2;
                for(; count<p; i++){
                        int tmp;
                        if(i==2){
                                sosuu[count]=i;
                                count++;
                                continue;
                        }
                        for(int j=2;j<m;j++){
                                if((tmp=i%j) == 0)break;
                        }
                        if(tmp != 0){
                                sosuu[count]=i;
                                count++; 
                        }
                        
                }
                count =0;
                for(i=0; i<p; i++){
                        for(int j=i; j<p; j++){
                                sum[count]=sosuu[i]+sosuu[j];
                                count++;
                        }
                }

                sort(sum, sum+count);
                cout << sum[p-1] << endl;
                cin >> n >> p;
        }
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int is[110001];
int prime[100000];
int use[10000];
int main(){
	is[0]=is[1]=-1;
	for(int i=2;i<110001;i++){
		if(!is[i]){
			is[i]=1;
			for(int j=i*2;j<110001;j+=i)is[j]=-1;
		}
	}
	int now=0;
	for(int i=0;i<110001;i++)if(~is[i])prime[now++]=i;
	int a,b;
	while(scanf("%d%d",&a,&b),~b){
		int at=upper_bound(prime,prime+now,a)-prime;
		int ind=0;
		for(int i=0;i<b;i++){
			for(int j=i;j<b;j++){
				use[ind++]=prime[at+i]+prime[at+j];
		//		printf("%d ",use[ind-1]);
			}
		}
		std::sort(use,use+ind);
		printf("%d\n",use[b-1]);
	}
}

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


using namespace std;

bool f[5000002];

int main(){
	for(int i=3;i<5000001;i+=2){f[i]=true;
		for(int j=3;j*j<=i;j+=2)
			if(i%j==0)f[i]=false;
	}
	f[2]=true;
	int n,p;
	while(1){
		cin>>n>>p;if(n==-1&&n==p)break;

		int r=0;
		int q[10001]={};
		int x=0;
		int y=n;
		while(1){
			if(f[y]==true)x++;
			if(x==p)break;
			y++;
		}
		n++;
		 vector<int> anslist;
		 for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
					anslist.push_back(i+j);
				
				cout<<i<<" "<<j<<endl;
				}
			}
			sort(anslist.begin(),anslist.end());
/*
		for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
				q[r]=i+j;r++;
				}
			}

		/*	
		sort(q,q+r);
		for(int i=0;i<r;i++)
		cout<<q[i]<<endl;

		cout<<q[p-1]<<endl;*/
			  cout << anslist[p-1] << endl;

	}
	
	return 0;
}