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;
const long long INF = 1e7+1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector < pair<ll, ll > > vp;
typedef vector <string> vs;
typedef vector <char> vc;
typedef list <ll> lst;

ll n,m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/

#define NUM 1000000
bool prime[NUM];

int main()
{
	REP(i, NUM)
	{
		prime[i] = true;
	}
	prime[0] = prime[1] = false;
	for (ll i = 2; i * i < NUM; i++) {
		if (prime[i]) {
			for (ll j = 2; i * j < NUM; j++) {
				prime[i*j] = false;
			}
		}
	}

	vi P;
	FOR(i, 2, NUM)
	{
		if (prime[i]) {
			P.push_back(i);
		}
	}

	while (1) {
		ll p;
		cin >> n >> p;
		if (n == -1 && p == -1) {
			return 0;
		} else {
			ll index = 0;
			REP(i, P.size())
			{
				if (n < P[i]) {
					index = i;
					break;
				}
			}
			vi Q;
			REP(i, p + 1)
			{
				FOR(j, i, p + 1)
				{
					Q.push_back(P[index + i] + P[index + j]);
				}
			}
			sort(ALL(Q));
			cout << Q[p - 1] << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int isPrime(int n){
  for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
  return 1;
}
int main(){
  int n,p,i,j,k,o,dp[200000]={};
  for(i=2;i<200000;i++) dp[i]=isPrime(i);
  cin >> n >> p;
  while(n!=-1){
    int c=0;
    int l[20]={};
    i=n;
    while(c<20){
      i++;
      if(dp[i]==1){
	l[c]=i;
	c++;
      }
    }
    vector <int> v;
    for(i=0;i<20;i++){
      for(j=i;j<20;j++){
	v.push_back(l[i]+l[j]);
      }
    }
    sort(v.begin(),v.end());
    cout << v[p-1] << endl;
    cin >> n >> p;
  }
  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

typedef long long ll;
typedef unsigned long long ull;

//?´???°??????
bool isPrime(int x) {
	if (x < 2)return 0;
	else if (x == 2)return 1;
	if (x % 2 == 0)return 0;
	for (int i = 3; i*i <= x; i += 2)
		if (x%i == 0)return 0;
	return 1;
}

//??¨????????????????????????
//n??\????????£??´??°????´???°???????????°???????????????is_prime[]???????´?
void eratos(int n, bool is_prime[]) {
	fill(is_prime, is_prime + n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i*i <= n; i++) {
		if (is_prime[i]) {
			int j = i + i;
			while (j <= n) {
				is_prime[j] = false;
				j = j + i;
			}
		}
	}
}

//?????§??¬?´???°
int gcd(int x, int y) {
	return y ? gcd(y, x%y) : x;
}

//???????´???? ??°??????????????????
ll mod_pow(ll x, ll n) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = res*x%MOD;
		x = x*x%MOD;
		n >>= 1;
	}
	return res;
}

static const int MAX_N = 100000;
static const int MAX_P = 100;

int main() {
	bool is_prime[MAX_N * 2];
	eratos(MAX_N * 2, is_prime);
	vector<int> prime;
	for (int i = 0; i <= MAX_N * 2; i++) {
		if (is_prime[i]) {
			prime.push_back(i);
		}
	}

	static vector<int> M[MAX_N + 1];
	for (int N, P; cin >> N >> P&&N != -1;) {
		if (M[N].size() == 0) {
			int mprime = lower_bound(prime.begin(), prime.end(), N) - prime.begin();
			if (is_prime[N])mprime++;
			for (int i = mprime; i < mprime + MAX_P; i++) {
				for (int j = i; j < mprime + MAX_P; j++) {
					M[N].push_back(prime[i] + prime[j]);
					//cout << prime[i] << " " << prime[j] << " " << prime[i] + prime[j] << endl;
				}
			}
			sort(M[N].begin(), M[N].end());
		}
		cout << M[N][P - 1] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int s[1000], k[10000], n, m, t, p, i, j;
int main() {
	while (true) {
		t = 0; p = 0;
		cin >> n >> m;
		if (n == -1) { break; }
		while (t <= m) {
			if (n < 2) { goto E; }
			for (i = 2; i <= sqrt(n); i++) {
				if (n%i == 0) {
					goto E;
				}
			}
			s[t] = n;
			t++;
		E:;
			n++;
		}
		for (i = 0; i <= m; i++) { for (j = i; j <= m; j++) { k[p] = s[j] + s[i]; p++; } }
		sort(k, k + p);
		cout << k[m - 1] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;

bool check(int x){
	if(x == 2)return true;
	if(x == 1 || x%2 == 0)return false;
	for(int i = 3;i <= sqrt(x);i+=2){
		if(x%i == 0)return false;
	}
	return true;
}

int main(){
	int n,p;
	vector<int> v,res;
	while(1){
		cin >> n >> p;
		if(n < 0)break;
		v.clear();
		res.clear();
		for(int i = n+1;;i++){
			if(check(i))v.push_back(i);
			if(v.size() > 100)break;
		}
		//for(int i = 0;i < 100;i++){
		//	cout << v[i] << endl;
		//}
		for(int i = 0;i < 100;i++){
			for(int j = i;j < 100;j++){
				res.push_back(v[i] + v[j]);
			}
		}
		sort(res.begin(),res.end());
		cout << res[p-1] << endl;

	}
	return 0;
}