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*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;
    ++N;
    while(1){
      if(is_prime(N)){
        prime.push_back(N);
        REP(j,prime.size()){
          matsuzaki.push_back(prime[j]+N);
        }
        if(matsuzaki.size()>=P){
          break;
        }
      }
      N++;
    }
    // cerr << "----prime----" << endl;
    // REP(i, prime.size()) {
    //   cerr << prime[i] << endl;
    // }
    sort(matsuzaki.begin(),matsuzaki.end());
    // cerr << "----matsuzaki----" << endl;
    // REP(i, matsuzaki.size()) {
    //   cerr << matsuzaki[i] << endl;
    // }
    cout << matsuzaki[P - 1] << endl;
  }    
}

C++: #include <bits/stdc++.h>

using namespace std;

bool pr[150000];

void erato(){
	fill(pr, pr+150000, true);
	pr[0] = pr[1] = false;
	
	for(int i=2; i < 150000; i++){
		if(!pr[i]) continue;
		for(int j=i+i; j < 150000; j+=i) pr[j] = false;
	}
}

int main(){
	erato();
	int n, p;
	while(cin >> n >> p, n+p!=-2){
		vector<int> prime;
		int count = 0;
		for(int i=n+1;; i++){
			if(pr[i]){ prime.push_back(i); count++;}
			if(count == 100) break;
		}
		
		vector<int> prime_w;
		for(int i=0; i < prime.size(); i++){
			for(int j=i; j < prime.size(); j++){
				prime_w.push_back(prime[i]+prime[j]);
			}
		}
		sort(prime_w.begin(), prime_w.end());
		
		cout << prime_w[p-1] << endl;
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string.h>

#define loop(lp) for(int lp2 = 0; lp2 < lp; lp2 ++) 
#define ascent(i,a,b) for(int i = a; i < b; i ++)

#define pb push_back

#define MAX 10000010

using namespace std;

bool flg[200000];
int  isprime[MAX];

int main() {
  //  vector<int> p1,p2;
  vector<int> p;

  ascent(i, 0, MAX) {
     isprime[i] = 1;
  }
   isprime[0] =  isprime[1] = 0;
  
  int cnt = 0;
  ascent(i, 2, MAX) {
    if(isprime[i]) {
      for(int j = 2*i; j <= MAX; j += i) {
	 isprime[j] = 0;
      }
    }
  }
  
  int N, P;
  while(cin >> N >> P) {
    if(N == -1 && P == -1) break;

    memset(flg, true, sizeof(flg));

    int k = N+1;
    int l = 0;
    p.clear();
    while(l < P) {
      if(isprime[k]) {
	p.pb(k);
	l ++;
      }
      k ++;
    }

    vector<int> vec;
    vec.clear();
    ascent(i, 0, p.size()) {
      ascent(j, i, p.size()) {
	  vec.pb(p[i] + p[j]);
	  //	  cout << p[i] + p[j] << endl;
      }
    }

    sort(vec.begin(), vec.end());
    cout << vec[P-1] << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;
bool prime[1000010];
void furui() {
    prime[0]=true;
    prime[1]=true;
	int i = 2;
	while (i < 1000010) {
		int j = 2;
		while (j <= sqrt(i) && !prime[i]) {
			if (i%j == 0) {
				prime[i] = true;
				break;
			}
			else j++;
		}
		int z = 2;
		while (!prime[i]) {
			if (i*z <= 1000010) {
				prime[i*z] = true;
				z++;
			}
			else break;
		}
		i++;
	}
}

int main(void){
    int n,p;
    furui();
    while(true){
        cin>>n>>p;
        if(n==-1&&p==-1)break;
        vector<int>P,ans;
        int num=0;
        for(int i=n+1;;i++){
            if(num>=100)break;
            if(!prime[i]){
                P.push_back(i);
                num++;
            }
        }
        for(int i=0;i<P.size();i++){
            for(int j=i;j<P.size();j++){
                ans.push_back(P[i]+P[j]);
            }
        }
        sort(ans.begin(),ans.end());
        cout<<ans[p-1]<<endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<algorithm>

using namespace std;

#define A 1000000
#define PRI 1002
#define PRIME 1000000

int main(int argc, char *argv[]){

  int N, P;
  int i, j, cnt=0;
  int prime[PRI], a[A];
  bool isprime[PRIME];



    fill(isprime, isprime+PRIME,true);
    isprime[0] = false;
    isprime[1] = false;

    for(i=2;i<PRIME+1;i++){
      isprime[i]=true;
    }
    
    cnt=0;
    for(i=2;i<PRIME+1;i++){
      if(isprime[i]){
	//	printf("%d is prime\n", i);
	for(j=2;j<PRIME/i;j++){
	  isprime[i*j]=false;
	}
      }
    }

  while(true){

    fill(a, a+A, 0);
    fill(prime, prime+PRI, 0);

    scanf("%d %d", &N, &P);
    if(N==-1 && P==-1){break;}

    cnt=0;
    for(i=0;i<PRIME;i++){
      if(i>N && cnt<1000 && isprime[i]){
	prime[cnt++] = i;
	//	printf("%d is selected prime\n", prime[cnt-1]);
      }
    }

    cnt=0;
    for(i=0;i<1000;i++){
      for(j=0;j<i+1;j++){
	a[cnt++] = prime[i]+prime[j];
	//     	printf("%d is %dth sum of two primes\n", a[cnt-1], cnt-1);
      }
    }


    sort(a, a+500500);

    for(i=0;i<10000;i++){
      //      printf("%dth sum is %d\n", i, a[i]);
    }


    printf("%d\n", a[P-1]);
  }

  return 0;
}