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 ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll PrimeMax = 1000001;
int is_prime[PrimeMax];
void Eratosthenes(){
  for(int i = 0; i < PrimeMax; i++){
    is_prime[i] = 1;
  }
  is_prime[0] = 0;
  is_prime[1] = 0;
  for(int i = 2; i < PrimeMax ; i++){
    if(is_prime[i]){
      for(int j = 0; i * (j + 2) < PrimeMax; j++){
        is_prime[i *(j + 2)] = 0;
      }
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  Eratosthenes();
  vector<ll> pr;
  rep(i,PrimeMax){
    if(i > 105000) break;
    if(is_prime[i] ){
      pr.push_back(i);
    }
  }
  /* std::cout << pr.size() << std::endl; */
  ll n,p;
  while(cin >> n >> p && (n+p > 0)){
    auto lw = upper_bound(all(pr),n);
    vector<ll> ans(150000);
    int k = 0;
    for(auto it = lw; it != pr.end();it++){
      for(auto it2 = it; it2 != pr.end();it2++){
        /* if(*it + *it2 > n * n && ans.size() > p) break; */
        if(k >= ans.size()) break;
        ans[k] = (*it + *it2);
        ++k;
      }
    }
    sort(ans.begin(),ans.begin()+k);
    /* rep(i,40){                        */
    /* std::cout << ans[i] << std::endl; */
    /* }                                 */
    cout << ans[p-1] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define Number 200000
using namespace std;

int main(){
  int n,p;
  vector<int> prime_table;
  vector<int> num_table(Number,1);
  num_table[0]=num_table[1]=0;
  for(int i=2;i<Number;i++){
    if(num_table[i]){
      prime_table.push_back(i);
      for(int j=i+i;j<Number;j+=i){
	num_table[j]=0;
      }
    }
  }
  while(cin>>n>>p,n+p>-1){
    vector<int> num;
    vector<int>::iterator it=upper_bound(prime_table.begin(),prime_table.end(),n);
    int index=distance(prime_table.begin(),it);
    p--;
    for(int i=index;i<index+100;i++){
      for(int j=index;j<=i;j++){
	num.push_back(prime_table[i]+prime_table[j]);
      }
    }
    sort(num.begin(),num.end());
    cout<<num[p]<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX_N (123456)
typedef vector<int> VI;
#define pb push_back
#define all(vec) (vec).begin(),(vec).end()

bool prime[MAX_N + 1];

int main()
{
	for (int i = 2; i <= MAX_N; i++) prime[i] = true;
	for (int i = 2; i * i <= MAX_N; i++)
		for (int j = i * i; j <= MAX_N; j += i)
			prime[j] = false;
	
	int n, p;
	while (scanf("%d %d", &n, &p), n != -1){
		VI v;
		for (int i = n + 1, j = 0; j < p; i++){
			if (prime[i]){
				v.pb(i);
				j++;
			}
		}
		VI vv;
		for (int i = 0; i < p; i++){
			for (int j = i; j < p; j++){
				vv.pb(v[i] + v[j]);
			}
		}
		sort(all(vv));
		
		for (int i = 0; i < vv.size(); i++){
			//printf("%d ", vv[i]);
		}
		
		printf("%d\n", vv[p - 1]);
	}
	
	return 0;
}

C++: #include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100000

int main(){
        int n, p, sosuu[100],count=0,sum[10000];
        string keymoji;

        cin >> n >> p;
        while(n != -1 && p != -1){
                int i=n;
                count = 0;
                if(i<2)i=2;
                for(; count<p; i++){
                        int tmp;
                        if(i==2){
                                sosuu[count]=i;
                                count++;
                                continue;
                        }
                        for(int j=2;j<1010;j++){
                                if((tmp=i%j) == 0)break;
                        }
                        if(tmp != 0){
                                sosuu[count]=i;
                                count++; 
                        }
                        
                }
                count =0;
                for(i=0; i<p; i++){
                        for(int j=i; j<p; j++){
                                sum[count]=sosuu[i]+sosuu[j];
                                count++;
                        }
                }

                sort(sum, sum+count);
                cout << sum[p-1] << endl;
                cin >> n >> p;

        }


}