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;

int n;
int p;
bool is_composite[11451419];
vector<int> primes;

int main() {
  is_composite[1] = true;
  for (int i=2; i<=11451418; i++) {
    if (!is_composite[i]) {
      primes.push_back(i);
      for (int j=i+i; j<=11451418; j+=i) {
        is_composite[j] = true;
      }
    }
  }

  while (1) {
    scanf("%d%d", &n, &p);
    if (n == -1 && p == -1) return 0;

    auto itr = upper_bound(primes.begin(), primes.end(), n);
    vector<int> nums;
    for (auto i=itr; i<=itr+p; i++) {
      for (auto j=i; j<=itr+p; j++) {
        nums.push_back(*i+*j);
      }
    }
    sort(nums.begin(), nums.end());
    printf("%d\n", nums[p-1]);
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define ERA_MAX 1000000
#define ROO_MAX 316
using namespace std;

bool Eratos[ERA_MAX];

int M(int N, int P)
{
	priority_queue<int> pq;
	vector<int> vec(P);
	for (int i = 0, n = N + 1; i < P; n++) {
		if (Eratos[n]) {
			vec[i] = n;
			i++;
		}
	}
	for (int i = 0; i < P; i++) {
		for (int j = i; j < P; j++) {
			pq.push(-(vec[i] + vec[j]));
		}
	}
	for (int i = 0; i < P - 1; i++)
		pq.pop();
	return -pq.top();
}

int main()
{
	int N, P;
	Eratos[0] = Eratos[1] = false;
	for (int i = 2; i < ERA_MAX; i++) {
		Eratos[i] = true;
	}
	for (int i = 2; i < ROO_MAX; i++) {
		for (int j = i * 2; j < ERA_MAX; j += i) {
			Eratos[j] = false;
		}
	}
	while (cin >> N >> P, N != -1 || P != -1) {
		cout << M(N, P) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstring>

#define MAX_N 5000000

using namespace std;

vector<int>  primes;
vector<bool> is_prime;

void Init()
{
  is_prime.resize(MAX_N + 1, true);
  for (int i = 2; i <= MAX_N; i++)  {
    if (is_prime[i]) {
      primes.push_back(i);
      for (int j = i * 2; j <= MAX_N; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int Func(int N, int P)
{
  vector<int> vi;
  int offset;
  for (offset = 0; offset < primes.size(); offset++) 
    if (primes[offset] > N)
      break;

  for (int i = 0; i < P; i++)
    for (int j = i; j < P; j++) {
      vi.push_back(primes[i + offset] + primes[j + offset]);
    }

  sort(vi.begin(), vi.end());
  return vi[P - 1];
}

int main()
{
  Init();
  int N, P;
  while (cin >> N >> P && N != -1 && P != -1) {
    cout << Func(N, P) << endl;
  }
}

C++: #include <bitset>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main()
{
    auto computePrimes = [](int n) {
        vector<int> r;
        bitset<500'000> b;
        auto t = b;
        t.set();
        t[0] = false;
        t[1] = false;
        int i = 0;
        while (true) {
            bool done = false;
            while (!t[i]) {
                ++i;
                if (i == t.size()) {
                    done = true;
                    break;
                }
            }
            if (done) {
                break;
            }

            b[i] = true;
            if (i > n) {
                r.push_back(i);
                if (r.size() >= 25) {
                    break;
                }
            }
            for (int j = i; j < t.size(); j += i) {
                t[j] = false;
            }
        }
        return r;
    };

    while (true) {
        int n, p;
        cin >> n >> p;
        if (n == -1 && p == -1) {
            return 0;
        }

        const auto primes = computePrimes(n);
        priority_queue<int, vector<int>, greater<>> q;
        for (int i = 0; i < primes.size(); ++i) {
            for (int j = i; j >= 0; --j) {
                q.push(primes[i] + primes[j]);
            }
        }
        for (int i = 0; i < p - 1; ++i) {
            q.pop();
        }
        cout << q.top() << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;


const long long int mod=1000000007;
const long long int INF=99999999999999999;

long long int N,P;

//素数判定　素数ならそのまま返す　合成数なら０を返す。
class Sosu{
	public:
	long long int sosu(long long int n) {
		for (int i = 2; i*i <= n; i++) {
			if (n%i == 0) { return 0; }
		}
		return n;

	}
};

int main() {
	
	cout << fixed << setprecision(18);
	cin>>N>>P;
	Sosu so;
	while(N!=-1){
		vector<long long int>vec,sos;
		for(int i=max(2LL,N+1);i==i;i++){
			if(so.sosu(i)==i){sos.push_back(i);}
			if(sos.size()>=100){break;}
		}
		for(int i=0;i<sos.size();i++){
			for(int j=i;j<sos.size();j++){
				vec.push_back(sos[i]+sos[j]);
			}
		}
		sort(vec.begin(),vec.end());
		cout<<vec[P-1]<<endl;
		for(int i=0;i<100;i++){
			//cout<<vec[i]<<" ";
		}
		//cout<<endl;

		cin>>N>>P;
	}

}