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 <algorithm>

using namespace std;

const int MAX_N = 100000;
int prime[MAX_N];
bool is_prime[MAX_N + 10000];

void sieve() {
	int p = 0;
	for (int i = 0; i < MAX_N + 10000; i++) {
		is_prime[i] = true;
	}
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i < MAX_N + 10000; i++) {
		if (is_prime[i]) {
			prime[p++] = i;
			for (int j = i * 2; j < MAX_N + 10000; j += i) {
				is_prime[j] = false;
			}
		}
	}
	return;
}

int matsuzaki_num(int n, int p) {
	int ans = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	int prime2[100];
	int prime_sum[5050];
	while(j < 100) {
		if (prime[i] > n) {
			prime2[j] = prime[i];
			j++;
		}
		i++;
	}
	
	for (i = 0; i < 100; i++) {
		for (j = i; j < 100; j++) {
			prime_sum[k++] = prime2[i] + prime2[j];
			//cout << prime_sum[k - 1] << endl;
		}
	}
	sort(prime_sum, prime_sum + 5050);
	ans = prime_sum[--p];
	
	return ans;
}

int main() {
	int n, p;
	sieve();
	while (true) {
		cin >> n >> p;
		if (n == -1) {
			break;
		}
		cout << matsuzaki_num(n, p) << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

vector<int> get_prime(int n){
    if(n <= 1) return vector<int>();
    vector<bool> iscomp(n+1);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!iscomp[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            if((ll)i*j > n) break;
            iscomp[i*j] = true;
            if(i % j == 0) break;
        }
    }
    return prime;
}
int main() {
    int n, p;
    auto x = get_prime(200000);
    while(cin >> n >> p, n != -1 && p != -1){
        vector<int> v;
        int cur = upper_bound(x.begin(), x.end(), n) - x.begin();
        for (int i = 0; i < p; ++i) {
            v.emplace_back(x[cur+i]);
        }
        vector<int> ans;
        for (int i = 0; i < p; ++i) {
            for (int j = i; j < p; ++j) {
                ans.emplace_back(v[i]+v[j]);
            }
        }
        sort(ans.begin(), ans.end());
        cout << ans[p-1] << "\n";
    }
    return 0;
}

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


vector<int> Eratosthenes(int N ){
    vector<bool> is_prime( N + 1 );
    for( int i = 0; i <= N; i++){
        is_prime[ i ] = true;
    }

    vector<int> P;
    for( int i = 2; i <= N; i++){
        if( is_prime[ i ] ){
            for( int j = 2 * i; j <= N; j += i ){
                is_prime[ j ] = false;
            }
            P.emplace_back( i );
        }
    }
    return P;
}

vector<int> make_list(int N, int P,vector<int> &prime){
    vector<int> dic;
    int idx = 0;
    auto itr = upper_bound(prime.begin(),prime.end(),N);
    idx = distance(prime.begin(),itr);

    for(int i=idx; i<=(idx+P); i++){
        for(int j=i; j<=(idx+P); j++){
            dic.push_back(prime[i]+prime[j]);
        }
    }

    return dic;
}



int main(){
    vector<int> prime = Eratosthenes(10000000);
    vector<int> ans;

    while(1){
        int N,P;
        cin>>N>>P;

        if(N == -1 && P == -1) break;
        vector<int> M = make_list(N,P,prime);
        sort(M.begin(),M.end());
        ans.push_back(M[P-1]);
    }

    for(auto a : ans) cout<<a<<endl;
    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;
    set<int>st;
    int k=s+1;
    for(int i=0;i<n/2;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;
      }
      st.insert(a[i]*2);
      //cout << a[i]*2<<endl;
    }
    int co=0;
    for( auto itr = st.begin(); itr != st.end() ; ++itr){
      co++;
      if(co==n-1){
	cout << *itr << endl;
	break;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#define N 150000
using namespace std;
int main(){
  int n,p,sosu[N],i,j,result,data[N],l,flag,temp;
  for(i=0;i<N;i++) sosu[i]=1;
  sosu[0]=0;
  sosu[1]=0;
  for(i=2;i<N/2;i++){
    if(sosu[i]==1){
      for(j=i*2;j<N;j+=i){
        sosu[j]=0;
      }
    }
  }
  while(1){
    flag=0;
    l=0;
    cin >> n >> p;
    if(n==-1&&p==-1) break;
    for(i=n+1;i<N;i++){
      if(sosu[i]==1){
	for(j=n+1;j<=i;j++){
          if(sosu[j]==1){
            data[l]=i+j;
	    l++;
	    if(l==p+100){
	      flag=1;
	      break;
	    }
	  }
	}
	if(flag==1) break;
      }
    }
    for(i=1;i<p+100;i++){
      for(j=1;j<p+100;j++){
	if(data[j]<=data[j-1]){
	  temp=data[j];
	  data[j]=data[j-1];
	  data[j-1]=temp;
	}
      }
    }
    cout << data[p-1] << endl;
  }
  return 0;
}