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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

vector<int> eratosthenes (int n){
    vector<int> primes(n);
    primes[0] = primes[1] = 0;
    for (int i = 2; i < n; i++) primes[i] = i;
    for (int i = 2; i < n; i++)
        if (primes[i])
            for (int j = i<<1; j < n; j += i) primes[j] = 0;
    vector<int>::iterator it = remove(primes.begin(), primes.end(), 0);
    primes.erase(it, primes.end());
    return primes;
}

int main(void){
    int n, p;
    vi el = eratosthenes(1000000);
    while(cin >> n >> p, n != -1){
        vi l, r;
        REP(i, el.size()){
            if(el[i] > n) l.push_back(el[i]);
            if(l.size() > p + 1) break;   
        }
        REP(i, l.size()){
            for(int j = i; j < l.size(); j++){
                r.push_back(l[i] + l[j]);
            }
        }
        sort(ALL(r));
        //r.erase(unique(ALL(r)), r.end());
        //REP(i, r.size()) cout <<  i<< "*" << r[i] << endl;


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

    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
using namespace std;

bool mprime[200001]={false};

void matuPrime(){
	mprime[0] = mprime[1] = true;
	for(int i=2;i*i<=200000;i++){
		for(int j=2;j<=200000/i;j++){
			mprime[j*i]=true;
		}
	}
}

int main(){
	int N,P;
	matuPrime();
	while(cin>>N>>P&&N!=-1){
		int count=0;
		vector<int> vt,sum;
		for(int i=N+1;count<100;i++){
			if(!mprime[i]){
				count++;
				vt.push_back(i);
				for(unsigned int j=0;j<vt.size();j++){
					sum.push_back(i+vt[j]);
				}
			}
		}
		sort(sum.begin(),sum.end());
		p(sum[P-1]);
	}
	return 0;
}

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

using namespace std;

bool is_prime(int32_t n) {
  if (n == 0 || n == 1) return false;

  for (uint32_t i = 2; i <= (uint32_t)sqrt((double)n); i++) {
    if (n % i == 0) return false;
  }
  return true;
}

vector<int32_t> primes(int32_t n) {
  int32_t count = 100;
  int32_t num = n + 1;
  vector<int32_t> ret;
  while (0 < count) {
    if (is_prime(num)) {
      ret.push_back(num);
      count--;
    }
    num++;
  }
  return ret;
}

int32_t main() {
  while (true) {
    int32_t N, P;
    cin >> N >> P;
    if (N == -1) break;

    vector<int32_t> ps = primes(N);
    vector<int32_t> sums;
    for (uint32_t i = 0; i < ps.size(); i++) {
      for (uint32_t j = i; j < ps.size(); j++) {
        sums.push_back(ps[i] + ps[j]);
      }
    }

    sort(sums.begin(), sums.end());

    cout << sums[P - 1] << endl;
  }

  return 0;
}

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

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;



int main(void) {
	int prime[20000] = {};
	int pos = 2;
	prime[1] = 2;
	FOR(i, 3, 200000) {
		bool flag = true;
		FOR(t, 2, sqrt(i) + 1) {
			if (i%t == 0) {
				flag = false;
				break;
			}
		}
		if (flag) {
			prime[pos] = i;
			pos++;
		}
	}
	
	while (1) {
		int n, p;
		int now;
		int count = 0;
		vector<int>dp(1326, 0);

		cin >> n >> p;
		if (p == -1) {
			break;
		}
		FOR(i, 1, pos) {
			if (prime[i] > n) {
				now = i;
				break;
			}
		}
		FOR(i, now, now + 50) {
			FOR(t, i, now + 50) {
				dp[count] = prime[i] + prime[t];
				count++;
			}
		}
		/*FOR(i, 0, 1325) {
			cout << dp[i] << endl;
		}*/
		sort(dp.begin(), dp.end());
		/*cout << endl;
		FOR(i, 0, 1325) {
			cout <<i<<" "<< dp[i] << endl;
		}
		cout << endl;*/
		cout << dp[p - 1] << endl;
	}
	
}