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++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};
 
static const double EPS = 1e-8;
 
static const int MAX_N = 1000000;
bool isPrime[MAX_N];
 
int Matsuzaki(int N,int P){
    vector<int> prime;
    for(int i=N+1;i<=N+100000;i++){
        if(isPrime[i]){
            prime.push_back(i);
            if(prime.size() >= 100) break;
        }
    }
 
    vector<int> compo;
    for(int i=0;i<prime.size();i++){
        for(int j=i;j<prime.size();j++){
            compo.push_back(prime[i]+prime[j]);
        }
    }
 
    sort(compo.begin(),compo.end());
    //unique(compo.begin(),compo.end());
 
    return compo[P-1];
}
 
int main(){
    int N,P;
    memset(isPrime,-1,sizeof(isPrime));
 
    isPrime[0] = false;
    isPrime[1] = false;
 
    for(int i=2;i<MAX_N;i++){
        if(!isPrime[i]) continue;
 
        for(int j=i+i;j<MAX_N;j+=i){
            isPrime[j] = false;
        }
    }
 
    while(~scanf("%d %d",&N,&P)){
        if(N==-1) break;
        printf("%d\n",Matsuzaki(N,P));
    }
}

C++: #include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

void eratosthenes(vector<int> &n){	
	n[1] = 1;
	for (int i = 2;i <= (120000/2); i++){
		n[i * 2] = 1;
	}
	int p = 3;
	while(p * p <= 120000){
		for (int i = p; i <= (120000/p); i++){
			n[i * p] = 1;
		}
		p++;
		while(n[p] == 1)p++;
	}
	return;
}

bool isPrime(int n){
	if (n < 2) return false;
	if (n == 2) return true;
	for (int i = 2; i <= sqrt(n) + 1; i++){
		if (n % i == 0) return false;
	}
	return true;
}
		
int main(){
	vector<int> era(120000);
	eratosthenes(era);
	vector<int> Primelist(120001);
	int index = 1;
	for (int i = 1; i <= era.size(); i++){
		if (era[i] == 0){
			Primelist[index] = i;
			index++;
		}
	}
	while(1){
		int n,p; cin >> n >> p;
		if (n + p == -2) break;
		index = 1;
		while(n >= Primelist[index]){
			index++;
		}
		
		vector<int> list(120000);
		for (int i = 1; i <= p; i++){
			for (int j = i; j <= p; j++){
				list[(i-1) * p + j] = Primelist[index + i - 1] + Primelist[index + j - 1];
			}
		}
		sort(list.begin(),list.end());
		index = 0;
		while(list[index] == 0){
			index++;
		}
		cout << list[index + p - 1] << endl;
		
	}

return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int a[10000]={2,3},i,j,k,c=2,b[100002][101],q,w[402];
int main(){
  for(i=4;i<150000;i++){
    for(k=1,j=2;j*j<=i;j++){
      if(i%j==0){k=0;break;}
    }
    if(k)a[c++]=i;
  }k=0;
  for(i=0;i<=100000;i++){
    if(i>a[k])k++;
    for(q=0,j=k;j<k+20;j++){
      for(c=j;c<j+20;c++){
        w[q++]=a[j]+a[c];
      }
    }
    sort(w[i],w[i]+q-1);
    for(c=0;c<=100;c++)b[i][c]=w[i];
  }
  while(cin>>i>>j){
    if(i<0)break;
    cout<<b[i][j-1]<<endl;
  }
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>

#define MEMZERO(x) memset(x, 0, sizeof(x))
#define MEMSET(x, y) memset(x, y, sizeof(x))

using namespace std;

const int SIZE = 500200;
bool isPrime[SIZE];
int prime[SIZE/2], pSize;
int table[5100];

void setPrime( void )
{
	pSize = 0;
	for(int i=2; i < SIZE; ++i) isPrime[i] = true;
	isPrime[0] = isPrime[1] = false;
	for(int i=2; i < SIZE; ++i) {
		if(!isPrime[i]) continue;
		prime[pSize++] = i;
		for(int j=i+i; j < SIZE; j+=i) {
			isPrime[j] = false;
		}
	}
}

int search_index( int num )
{
	int l=0, h = pSize;
	while(l < h) {
		int m = (l + h) / 2;
		if(prime[m] < num) l = m + 1;
		else if(prime[m] > num) h = m;
		else return (m+1);
	}
	return l;
}

int main( void )
{
	setPrime();
	int m, p;
	while(cin >> m >> p, m != -1 || p != -1) {
		MEMZERO(table);
		int ii = search_index(m);
		int count = 0;
		for(int i=0; i < 100; ++i)
			for(int j=i; j < 100; ++j) {
				table[count++] = prime[i+ii] + prime[j+ii];
			}
		sort(table, table+count);
		cout << table[p-1] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

#define PMAX 200000

using namespace std;

int main(){
	vector<int> prime;
	bool pr[PMAX]={0};
	pr[0] = true; pr[1] = true;
	for(int i=2; i<PMAX; i++){
		if(!pr[i]){
			prime.push_back(i);
			for(int j=2*i; j<PMAX; j+=i){
				pr[j] = true;
			}
		}
	}
	int N, P;
	while(cin >> N >> P, N != -1 && P != -1){
		vector<int> sum;
		int j;
		for(j=0; j<prime.size(); j++){
			if( prime[j] > N ){
				break;
			}
		}
		for(int i=0; i<P; i++){
			for(int k=0; k<=i; k++){
				sum.push_back( prime[j+i] + prime[j+k] );
			}
		}
		sort(sum.begin(), sum.end());
		cout << sum[P-1] << endl;
	}
	return 0;
}