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<vector>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  int n,p;
  while(cin >> n >> p){
    if(n==-1)break;
    int m,sosu[200]={},count=0,judge=1;
    for(m=n+1;count<=200;m++){
      for(int i=2;i<m;i++){
        if(m%i==0){
          judge=0;
        }
      }
      if(judge==1&&m!=1){
        sosu[count]=m;
        count++;
      }
      else judge=1;
    }
    vector<int> sum(200);
    int COUNT=0;
    for(int i=0;i<10;i++){
      for(int j=i;j<25;j++){
        sum[COUNT]=sosu[i]+sosu[j];
        COUNT++;
      if(COUNT==200)break;
      }
      if(COUNT==200)break;
    }
    sort(sum.begin(),sum.end());
    cout << sum[p-1] << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <time.h>
#include <tuple>
#define all(c) ((c).begin(),(c).end())
#define rall(c) ((c).rbegin(),(c).rend())
//#define sort(v,n) sort(v,v+n)
#define ll long long
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
bool a[300000];
inline void sosuu() {
	for (int i = 0; i < 300000; i++) {
		a[i] = 1;
	}
	int now = 2;
	while (now<=150000) {
		if (a[now]) {
			for (int i = now * 2; i < 300000; i += now) {
				a[i] = 0;
			}
		}
		now++;
	}
	a[0] = 0;
	a[1] = 0;
}


int main() {
	sosuu();
	int n, p;
	while (cin >> n >> p) {
		if (n == -1)return 0;
		int cont = 0;
		bool flag = 0;
		vector<int> v;
		for (int i = n+1;i<300000; i++) {
			for (int j = n+1;j<=i; j++) {
				if (a[i] && a[j]) {
					cont++;
					v.push_back(i + j);
				}
				else {
					continue;
				}
				if (cont == p+1000) {
					flag = 1;
					break;
				}
			}
			if (flag)break;
		}
		sort(v.begin(), v.end());
		//for (int i = 0; i < v.size(); i++)cout << "v" << v[i] << endl;
		cout << v[p - 1] << endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define PRIMENUM 1000100

int isPrime[PRIMENUM];
int primeLine[PRIMENUM];

void makePrimeNumber(){

	isPrime[2]=1;
	isPrime[3]=1;

	for(int i=4;i<PRIMENUM;i++){
		int flag=0;
		for(int j=2;j*j<=i;j++){
			if(i%j==0){
				flag=1;
				break;
			}
		}
		if(flag==0){
			isPrime[i]=1;
			//cout<<i<<endl;
		}	
	}

}

void makePrimeLine(){

	int pos=0;

	for(int i=0;i<PRIMENUM;i++){
		if(isPrime[i]==1){
			primeLine[pos]=i;
			pos++;
		}
	}
}

void swap(int *a,int *b){
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void babbleSort(int *a,int num){
	for(int i=0;i<num;i++){
		for(int j=num-1;j>i;j--){
			if(a[j]<=a[j-1])swap(&a[j],&a[j-1]);
		}
	}
}

int main(){

	int n,p;
	makePrimeNumber();
	makePrimeLine();

	while(cin>>n>>p){
		if(n==-1&&p==-1)break;

		int *ans;
		ans = new int[500];

		for(int i=0;i<500;i++){
			ans[i]=0;
		}

		//primeLineのnの場所を探索する。
		
		int pos=0;

		//まずどの素数を見るか？
		for(int i=n+1;i<PRIMENUM;i++){
			if(isPrime[i]>0){
				pos=i;
				break;
			}
		}
		//そしてその素数がどこにあるのか。
		for(int i=0;i<PRIMENUM;i++){
			if(primeLine[i]==pos){
				pos=i;
			//	cout<<primeLine[pos]<<endl;
				break;
			}
		}

		int ansPos=0;
		for(int i=pos;i<PRIMENUM;i++){
			int insFlag=0;
			for(int j=pos;j<=i;j++){
				insFlag=0;
				if(insFlag==0){
					ans[ansPos]=primeLine[i]+primeLine[j];
				//	cout<<"i="<<i<<"p["<<i<<"]="<<primeLine[i]<<"j="<<j<<" "<<primeLine[j]<<endl;
					ansPos++;
				}
				if(ans[500-1]>0){
					insFlag=5;
					break;
				}
					
			}
			if(insFlag==5)break;
		}

		babbleSort(ans,500);

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

	return 0;
}

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

int n, p;

bool isPrime(int x) {
	if (x <= 1) return false;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) return false;
	}
	return true;
}

int main() {
	while (cin >> n >> p) {
		if (n == -1) break;
		
		vector<int> nums;
		for (int i = n + 1; nums.size() < p; i++) {
			if (isPrime(i)) {
				nums.push_back(i);
			}
		}
		
		vector<int> sums;
		for (int i = 0; i < nums.size(); i++) {
			for (int j = i; j < nums.size(); j++) {
				sums.push_back(nums[i] + nums[j]);
			}
		}
		sort(sums.begin(), sums.end());
		
		cout << sums[p - 1] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int s[1000], k[10000], n, m, t, p, i, j;
int main() {
	while (true) {
		t = 0; p = 0;
		cin >> n >> m;
		while (t <= m) {
			if (n < 2) { goto E; }
			for (i = 2; i <= sqrt(n); i++) {
				if (n%i == 0) {
					goto E;
				}
			}
			s[t] = n;
			t++;
		E:;
			n++;
		}
		for (i = 0; i < m; i++) { for (j = i; j < m; j++) { k[p] = s[j] + s[i]; p++; } }
		sort(k, k + p);
		cout << k[m] << endl;
	}
	return 0;
}