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 "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, H, W, L, R;

list<int> Prime(int num) {
	list<int>P;
	P.push_back(2);
	P.push_back(3);
	for (int i = 5; i <= num; i += 6) {
		bool flag = true;
		for (auto j : P) {
			if (j*j > i) {
				break;
			}
			if (i%j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i);
		flag = true;
		for (auto j : P) {
			if (j*j > i + 2) {
				break;
			}
			if ((i + 2) % j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i + 2);
	}
	return P;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	list<int>P = Prime(102000);
	cin >> N >> M;
	while (N!=-1) {
		vector<int>box(M);
		int bag = 0;
		for (auto i : P) {
			if (i > N) {
				box[bag] = i;
				bag++;
			}
			if (bag == M) {
				break;
			}
		}
		vector<int>ans;
		for (int i = 0; i < M; i++) {
			for (int j = i; j < M; j++) {
				ans.push_back(box[i] + box[j]);
			}
		}
		sort(ans.begin(), ans.end());
		cout << ans[M - 1] << endl;
		cin >> N >> M;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

vi prime;
bool isP[300000];

int main() {
	for(int i=2; i*i<300000; i++) {
		if(!isP[i]) {
			for(int j=i+i; j<300000; j+=i) {
				isP[j] = true;
			}
		}
	}
	for(int i=2; i<300000; i++) {
		if(!isP[i]) {
			prime.push_back(i);
		}
	}
	int n, p;
	while(cin >> n >> p, n!=-1 && p!=-1) {
		int s = 0;
		while(prime[s] <= n) s++;
		vi adds;
		for(int i=s; i<100+s; i++) {
			for(int j=i; j<100+s; j++) {
				adds.push_back(prime[i]+prime[j]);
			}
		}
		sort(adds.begin(), adds.end());
		cout << adds[p-1] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int prime[MAX];
bool isprime[MAX];

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

int main(){
    sieve(303030);
    while(1){
        ll n,p; cin >> n >> p;
        if(n==-1 && p==-1) break;
        vl v;
        ll cnt = p;
        for(ll i=n+1;; i++){
            if(isprime[i]){
                v.push_back(i);
                cnt--;
            }
            if(!cnt) break;
        }
        vl sum;
        rep(i,v.size()){
            REP(j,i,v.size()){
                sum.push_back(v[i]+v[j]);
            }
        }
        sort(all(sum));
        cout << sum[p-1] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i = 0; i < n; i++)
int main(){
	int p[110000];
	p[0] = 0;
	p[1] = 0;
	p[2] = 1;
	p[3] = 1;
	p[4] = 0;
	for(int i = 5; i < 110000; i+=2){
		bool f = 1;
		for(int j = 3; j*j <= i; j+=2){
			if(i % j == 0)f = 0;
		}
		p[i] = f;
	}
	int N,P;
	int ps[100];
	cin >> N >> P;
	int ans[1000];
	int c;
	while(N!= -1){
		c = 0;
		for(int i = N+1; c < 100; i++){
			if(p[i]){
				ps[c] = i;
				c++;
			}
		}
		for(int i = 0; i < 1000; i++)ans[i]=1e9;
		c = 0;
		
		for(int i = 0; i < 100; i++){
			for(int j = i; j < 100; j++){
				if(c < 1000)ans[c] = ps[i]+ps[j];
				c++;
			}
		}
		sort(ans, ans+1000);
		cout << ans[P-1] << endl;
		cin >> N >> P;
	}
	return 0;
}