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 <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
//typedef pair<int, int> P;
struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
};

/** Problem2150 : Matsuzaki Number **/
const int MAX = 1e6;
int main()
{
	bool isPrime[MAX];
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0]=false, isPrime[1]=false;
	for (int i=2; i<MAX; i++) {
		if (isPrime[i]) {
			for (int j=2; i*j<MAX; j++) {
				isPrime[i*j]=false;
			}
		}
	}
	
	while (true) {
		int N, P;
		cin>>N>>P;
		if (N==-1&&P==-1) break;
		
		vector<int> pp;
		for (int i=N+1; i<MAX&&pp.size()<=P; i++) {
			if (isPrime[i])
				pp.push_back(i);
		}
		
		vector<int> m;
		for (int i=0; i<pp.size(); i++) {
			for (int j=i; j<pp.size(); j++) {
				m.push_back(pp[i]+pp[j]);
			}
		}
		
		sort(m.begin(), m.end());
		cout << m[P-1] << endl;
	}
}

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;
		if (n == 0) { break; }
		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;
}

C++: #include <bits/stdc++.h>
using namespace std;

void print(vector<int> V){
  for(vector<int>::iterator p = V.begin(); p != V.end(); p++){
    cout << *p << " ";
  }
  cout << endl;
}

int sosu(int n){
  if(n==2) return 1;
  if(n==1||n==0||n%2==0) return 0;
  for(int i = 3; i <= sqrt(n); i+=2){
    if(n%i == 0) return 0;
  }
  return 1;
}

int main(){

  while(1){
    int N,P;
    cin >> N >> P; if(N==-1&&P==-1) break;

    vector<int> V;

    for(int i = 1; i < 10000; i++){
      if(sosu(N+i) == 1){
        V.push_back(N+i);
      }
    }

    //print(V);

    vector<int> V2;

    for(int i = 0; i < V.size(); i++){
      for(int j = 0; j <= i; j++){
        V2.push_back(V[i] + V[j]);
      }
    }
    sort(V2.begin(),V2.end());
    //print(V2);
    cout << V2[P-1] << endl;
  }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){

  vector <int> so,sum;
  int a[101001]={0},from,n,p,i,j;
  
   for(i=2;i<=101000;i++){
    if(a[i]==0){
      so.push_back(i);
      for(j=i;j<=101000;j+=i) a[j]=1;
    }
   }
   //for(i=0;i<20;i++) cout<<so[i]<<endl;
   while(cin>>n>>p,n!=-1){
  from=0;
  for(i=0;n>=so[i];i++) from++;
  //cout<<from<<endl;
  sum.clear();
  for(i=from;i<from+2*sqrt(p);i++) for(j=i;j<from+2*sqrt(p);j++) sum.push_back(so[i]+so[j]);
  sort(sum.begin(),sum.end());

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

}