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 <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };



const int MAX_PRIME = 114514;;
bool is_prime[MAX_PRIME + 1];
void sieve(int n = MAX_PRIME)
{
	fill_n(is_prime, n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i * i <= n; ++i)
	{
		if (is_prime[i])
		{
			for (int j = 2 * i; j <= n; j += i)
				is_prime[j] = false;
		}
	}
}
vector<int> make_primes(int n = MAX_PRIME)
{
	vector<int> res;
	for (int i = 2; i <= n; ++i)
		if (is_prime[i])
			res.push_back(i);
	return res;
}
int main()
{
	sieve();
	vector<int> primes = make_primes();
	
	int n, p;
	while (scanf("%d%d", &n, &p), ~n)
	{
		vector<int> w;
		int t = upper_bound(all(primes), n) - primes.begin();
		for (int i = t; i < t + p; ++i)
			for (int j = i; j < t + p; ++j)
				w.push_back(primes[i] + primes[j]);
		sort(all(w));
		printf("%d\n", w[p - 1]);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define all(c) (c).begin(), (c).end()

template<typename T> inline T sqr(T x){return x * x;}

void sieve(vector<int>& v, int n){
    v.assign(n, 0); iota(all(v), 2);
    for(auto div=v.begin(); sqr(*div)<=v.back(); ++div){
        v.erase(remove_if(div+1, v.end(), [&](int x){return x % *div == 0;}), v.end());
    }
}

vector<int> primes;

int solve(int N, int P){
    int k = upper_bound(all(primes), N) - primes.begin();
    vector<int> M;
    for(int i=k; i<k+P; ++i){
        for(int j=k; j<=i; ++j)M.push_back(primes[i] + primes[j]);
    }
    sort(all(M));
    return M[P-1];
}

int main(){
    sieve(primes, 120000);
    int N, P;
    while(cin >> N >> P, ~N|~P)cout << solve(N, P) << '\n';
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>

using namespace std;


int prime[100000000];

bool is_Prime[100000000];

int ans[1010];


int main(){

  for(int i = 2 ; i < 100000000 ; i++){
    is_Prime[i] = true;
  }

  for(int i = 2 ; i < 100000000 ; i++){
    if(is_Prime[i]){
      for(int j = 2; i*j < 100000000 ; j++)
	is_Prime[i*j] = false;
    }
  }

  for(int i = 2,j = 0; i < 100000000 ; i++){
    if(is_Prime[i] ){
      prime[j] = i;
      j++;
    }
  }

  for(int i = 0; i < 100; i++){
    //  cout << prime[i] << " ";
  
  }

  int N,P;

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

    int i;
    for(i = 0; prime[i] <= N ;i++){}
    // prime[i] テ」ツ?ッNテ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??、ツクツ?ァツ閉ェテ・ツーツ湘」ツ?陛」ツ?ェテァツエツ?ヲツ閉ー 
    // cout << i << " " ;



    int t,s,x = 0;

    for(int k = i,t = 0 ; t < 40 ; t++,k++){
      for(int l = k,s = k-i ; s < 40 ; s++,l++){
	ans[x] = prime[k]+prime[l];
	//		cout << ans[x] << " ";
	x++;
      }
      //     cout << endl;
    }

    sort(ans ,ans + 500);


    for(i = 0; i < 100 ; i++){
      //    cout << ans[i] << " ";
    }
    //    cout << endl;    
       cout << ans[P-1] << endl;

  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#define pb push_back
#define all(v) v.begin(),v.end()
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define MAXBOOL 500000
using namespace std;

int main(){
	int n,p;
	vector<int> prime;
	bool check[MAXBOOL];
	while(cin>>n>>p,n!=-1||p!=-1){
		prime.clear();
		fill(check,check+MAXBOOL,true);
		check[0]=check[1]=false;
		loop(i,2,sqrt(MAXBOOL)){
			for(int j=i*2;j<MAXBOOL;j+=i){
				check[j]=false;
			}
		}
		int cnt=0;
		while(prime.size()<=200){
			if(check[n+1+cnt])prime.pb(n+1+cnt);
			cnt++;
		}
		vector<int> ans;
		rep(i,prime.size()){
			loop(j,i,prime.size()){
				ans.pb(prime[i]+prime[j]);
			}
		}
		sort(all(ans));
		cout<<ans[p-1]<<endl;
	}
	return 0;
}