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>
#include<vector>
using namespace std;

const int MAX_N = 1e7 + 1;

vector<bool> is_prime;

vector<bool> hurui() {
    vector<bool> res(MAX_N, true);
    res[0] = res[1] = false;
    for (int i = 2; i < MAX_N; i++) {
        if (!res[i]) continue;
        int idx = i * 2;
        while (idx < MAX_N) {
            res[idx] = false;
            idx += i;
        }
    }
    return res;
}

void M(int n, int p) {
    vector<int> primes;
    int cnt = 100;
    for (int i = n + 1; i < MAX_N; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
            cnt--;
        }
        if (cnt == 0) break;
    }
    vector<int> prime_sum;
    for (int i = 0; i < primes.size(); i++) {
        for (int j = i; j < primes.size(); j++) {
            prime_sum.push_back(primes[i] + primes[j]);
        }
    }
    sort(prime_sum.begin(), prime_sum.end());
    cout << prime_sum[p - 1] << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, P;
    is_prime = hurui();
    while (true) {
        cin >> N >> P;
        if (N < 0 && P < 0) break;
        M(N, P);
    }
    return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;

#define INF 1000000000
#define EPS 1e-10

#define MAX_N 100000
#define MAX_P 100

int N, P;

bool prime[5 * MAX_N];

vector<int> ans_list;

int main(){

	fill(prime, prime + 5 * MAX_N, true);

	prime[0] = prime[1] = false;
	for (int i = 0; i < 5 * MAX_N; i++){
		if (prime[i]){
			for (int j = 2 * i; j < MAX_N * 5; j += i){
				prime[j] = false;
			}
		}
	}

	while (true){

		cin >> N >> P;
		if (N == -1 && P == -1)
			break;

		vector<int> L;
		for (int i = N + 1, count = 0; count < 100; i++){
			if (prime[i]){
				L.push_back(i);
				count++;
			}
		}

		vector<int> ans;
		for (int i = 0; i < L.size(); i++){
			for (int j = i; j < L.size(); j++){
				ans.push_back(L[i] + L[j]);
			}
		}

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

		ans_list.push_back(ans[P - 1]);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
bool isPrime(int x){
    if(x < 2) return false;
    if(x == 2) return true;
    if(x % 2 == 0) return false;
    for(int i=3; i*i<=x; i+=2){
        if(x % i == 0) return false; 
    }
    return true;
}
int main(){
    int n,p;
    while(cin >> n >> p){
        if(n == -1 && p == -1) break;
        vector<int> vec;
        for(int i=n+1; i<=n+1000; i++){
            if(isPrime(i)){
                for(int j=i; j<=n+1000; j++) if(isPrime(j)) vec.push_back(i+j);
            }
        }
        sort(vec.begin(),vec.end());
        cout << vec[p-1] << endl;
    }
    return 0;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#pragma comment (linker, "/STACK:526000000")

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(10);
}

#define int ll
void solve() {
	while (true) {
		int n, p;
		cin >> n >> p;
		if (n == -1) return;
		vector<int> inputs;
		for (int i = n + 1; i <= n + 1000; ++i) {
			int ok = 1;
			for (int q = 2; q * q <= i; ++q) {
				if (i % q == 0) {
					ok = 0;
				}
			}
			if (ok == 1) inputs.push_back(i);
		}
		vector<int> hoge;
		REP(i, inputs.size()) {
			for (int q = i; q < inputs.size(); ++q) {
				hoge.push_back(inputs[i] + inputs[q]);
			}
		}
		sort(ALL(hoge));
		cout << hoge[p - 1] << endl;
	}
}
#undef int

int main() {
	init();
	solve();
}