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 <cstdio>
#include <set>

using namespace std;

#define MAX 131071

int main()
{
	bool is_prime[MAX] = {false};

	for(int i = 3; i < MAX; i += 2) {
		is_prime[i] = true;
	}
	is_prime[2] = true;
	for(int i = 3; i * i < MAX; i += 2) {
		if(!is_prime[i]) continue;
		for(int j = i * 2; j < MAX; j += i) {
			is_prime[j] = false;
		}
	}
	
	while(1) {
		int N, P;
		multiset<int> set_sum_p;
		int first_p;
		int ans;

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

		for(int i = N + 1; i < MAX; i++) {
			if(!is_prime[i]) continue;
			first_p = i;
			break;
		}
		
		int a = first_p, b = first_p;
		while(a < MAX) {
			if(!is_prime[a]) {
				a++;
				continue;
			}

			if(set_sum_p.size() >= P) {
				multiset<int>::iterator it = set_sum_p.begin();
				for(int i = 1; i < P; i++) {
					it++;
				}
				if(a + first_p > *it) {
					ans = *it;
					break;
				}
			}
			
			b = first_p;
			while(b <= a) {
				if(!is_prime[b]) {
					b++;
					continue;
				}
				set_sum_p.insert(a + b);
				b++;
			}
			a++;
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX = 1001000;

int main() {
	bool is_p[MAX] = {0};
	for ( int i = 2; i < MAX; i++ ) {
		for ( int j = 2; j*i < MAX; j++ ) {
			is_p[i*j] = true;
		}
	}

	vector<int> p;	
	for ( int i = 2; i < MAX; i++ ) {
		if ( !is_p[i] ) p.push_back(i);
	}
		
	int N, P;
	while ( cin >> N >> P, N != -1 ) {
		vector<int> v;
		int id = upper_bound(p.begin(), p.end(), N)-p.begin();
		//cout << p[id] << endl;
		for ( int i = id; i < id+110; i++ ) {			
			for ( int j = i; j < id+110; j++ ) {
				v.push_back(p[i]+p[j]);
			}
		}
		sort(v.begin(), v.end());
		cout << v[P-1] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    bitset<200000> sieve;
    sieve.flip();
    sieve[1] = 0;
    for (ll i = 2; i < 200000; i++) for (ll j = i * i; j < 200000; j += i)
        sieve[j] = 0;

    while (true) {
        int n, p; cin >> n >> p;
        int u = p * 2 + 5;
        if (n == -1) break;
        vi v, v2;
        for (int i = n + 1; v.size() < u; i++) if (sieve[i]) v.push_back(i);
        // cout << "? "; for (int h : v) cout << h << " "; cout << endl;
        rep(i, 0, u) rep(j, i, u) v2.push_back(v[i] + v[j]);
        sort(all(v2));
        // for (int h : v2) cout << h << " "; cout << endl;
        cout << v2[p - 1] << endl;
    }
}