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 <bits/stdc++.h>
using namespace std;
 
using ll=long long;
#define int ll
 
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
 
int read(){
    int i;
    scanf("%lld",&i);
    return i;
}
 
bool is_prime(int n){
    if(n==0||n==1) return false;
    if(n==2) return true;
    for(int i=2;i<n;i++){
        if(n%i==0) return false;
    }
    return true;
}
 
signed main(){
    while(true){
        int N=read(),P=read();
        if(N==-1&&P==-1) break;
        vector<int> prime;
        vector<int> matsuzaki;
        while(1){
            if(is_prime(N)){
                prime.push_back(N);
                REP(j,prime.size()){
                    matsuzaki.push_back(prime[j]+N);
                }
                sort(matsuzaki.begin(),matsuzaki.end());
                if(prime.size()>=P+10){
                    //REP(i,prime.size()) cout << prime[i] << endl;
                    cout << matsuzaki[P-1] << endl;
                    break;
                }
            }
            N++;
        }
    }
     
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int L = 1e7;
const int M = 30;

bool p[L];

void make_p(){
  REP(i, L) p[i] = true;
  p[0] = p[1] = false;
  for(int i = 2; i * i <= L; ++i){
    if(!p[i]) continue;
    for(int j = i + i; j < L; j += i) p[j] = false;
  }
}

int main() {
  make_p();
  int N, P;
  while(cin >>N >>P && N != -1){
    vector<int> _p(M), v;
    for(int i = N + 1, cnt = 0; cnt < M; ++i) if(p[i]) _p[cnt++] = i;
    REP(i, M) FOR(j, i, M) v.push_back(_p[i] + _p[j]);
    SORT(v);
    cout <<v[P - 1] <<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
int main(void){
    int prime[300010]={};
    int b=0;
    for(int i=2;i<=200274;i++){
        if(prime[i]==0){
            prime[i]=1;
            b++;
            for(int j=2*i;j<=200274;j+=i){
                prime[j]=-1;
            }
        }
    }
    vector<int> p(b);
    int a=0;
    for(int i=0;i<200274;i++){
        if(prime[i]==1){
            p[a]=i;
            a++;
        }
    }
    int s[200275];
    while(1){
        int n,m;
        cin>>n>>m;
        if(n==-1&&m==-1)break;
        for(int i=0;i<200275;i++)s[i]=0;
        int c;
        for(int i=0;i<200275;i++){
            if(p[i]>n){
                c=i;
                break;
            }
        }
        for(int i=c;i<c+m;i++){
            for(int j=c;j<c+m;j++){
                int sum=p[i]+p[j];
                if(sum<=200274)s[sum]++;
            }
        }
        int t=0;
        for(int i=4;i<200275;i++){
            t+=s[i];
            if(t>=m){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int main(){
  int so[200000]={};
  int f;
  so[2]=1;
  for(int i=2;i<200000;i++){
    f=0;
    for(int j=2;j*j<=i;j++){
      if(i%j==0){
	f=1;
	break;
      }
    }
    if(f==0)so[i]=1;
  }
  int s,n;
  //
  while(1){
    cin >> s >> n;
    if(s==-1&&n==-1)break;
    vector<int>a;
    multiset<int>st;
    int k=s+1;
    for(int i=0;i<=n;i++){
      while(1){
	if(so[k]==0)k++;
	else{
	  a.push_back(k);
	  k++;
	  break;
	}
      }
    }
    int l=0;
  
    for(int i=0;i<a.size();i++){
      for(int j=0;j<=i;j++){
	st.insert(a[i]+a[j]);
	//	  cout << a[i]+a[j]<<endl;
      }
      //cout << a[i]*2<<endl;
    }
    int co=0;

    for( set<int>::iterator itr = st.begin(); itr != st.end() ; ++itr){
      co++;
       if(co==n){
	cout << *itr << endl;
		break;
	 }
    }
  }
  return 0;
}

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

bool e[100010];
int primeNumbers[150010];

void erato(){
	fill(e, e+150010, true);
	e[0] = e[1] = false;

	for(int i=0; i < 150010; i++){
		if(!e[i]) continue;
		for(int j=i+i; j < 150010; j += i){
			e[j] = false;
		}
	}
}

int main(){
	erato();

	int n, p;
	int primeNumbers[100];
	int addPrimeNumbers[100010];
	while(cin >> n >> p, n != -1 && p != -1){
		while(1){
			if(e[++n]) break;
		}
		for(int i=0; i < p; n++){
			if(e[n]){
				primeNumbers[i] = n;
				i++;
			}
		}
		int countPrimeNumbers = 0;
		for(int i=0; i < p; i++){
			for(int j=i; j < p; j++){
				addPrimeNumbers[countPrimeNumbers] =  primeNumbers[i] + primeNumbers[j];
				countPrimeNumbers++;
			}
		}
		sort(addPrimeNumbers, addPrimeNumbers + countPrimeNumbers);

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