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 <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define SZ 110000
vector<int> prime;
bool unprime[SZ];
int main(){
  for(int i = 2; i < SZ; i++){
    if(!unprime[i]){
      for(int k = i*2; k < SZ; k+=i){
        unprime[k] = true;
      }
      prime.push_back(i);
    }
  }
  int N,P;
  while(cin>>N>>P){
    if(N==-1) break;
    vector<int>::iterator it = upper_bound(prime.begin(),prime.end(), N);
    vector<int> subprime;
    for(int i = 0; i<=P; i++){
      subprime.push_back(*it++);
    }
    vector<int> sumof2;
    REP(i,subprime.size())FOR(j,i,subprime.size())
      sumof2.push_back(subprime[i]+subprime[j]);
    sort(sumof2.begin(),sumof2.end());
    cout<<sumof2[P-1]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int prime[200000];
bool is_prime[200000];
int sieve(int n);
vector<int> prime_prime;
int main(){
  sieve(200000);
  int i,j;
  int n,p;
  int count=0;
  while(cin>>n>>p){
    count=0;
    prime_prime.clear();
    if(n==-1 && p==-1)break;
    while(prime[count]<=n)count++;
    for(i=count;i<p+count;i++)
      for(j=i;j<p+count;j++)
	prime_prime.push_back(prime[i]+prime[j]);
    sort(prime_prime.begin(),prime_prime.end());
    //for(int i=0;i<num;i++)cout<<prime_prime[i]<<endl;    
    cout<<prime_prime[p-1]<<endl;
  }
  return 0;
}

int sieve(int n){
  int p = 0;
  int i,j;
  for(i = 0;i<=n;i++)is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for(i = 2;i<=n;i++){
    if(is_prime[i]){
      prime[p++]=i;
      for(j=2*i;j<=n;j+=i)is_prime[j]=false;
    }
  }
  return p;
}

C++: #include <bits/stdc++.h>

using namespace std;

int p[200001];

int main()
{
    p[0] = 1;
    p[1] = 1;
    for (int i = 4; i <= 200000; i += 2) p[i] = 1;
    
    for (int i = 3; i * i <= 200000; i += 2){
        if (!p[i]){
            for (int j = i * 3; j <= 200000; j += 2 * i) p[j] = 1;
        }
    }
    
    int N, P;
    
    while (scanf("%d %d", &N, &P) && ~N){
        vector<int> v, v2;
        
        for (int i = N + 1; ; i++){
            if (!p[i]) v.push_back(i);
            if (v.size() == 100) break;
        }
        
        for (int i = 0; i < 100; i++){
            for (int j = i; j < 100; j++){
                v2.push_back(v[i] + v[j]);
            }
        }
        sort(v2.begin(), v2.end());
        
        printf("%d\n", v2[P - 1]);
    }
    
    return (0);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MX = 1301082;
bool sieve[MX];

void makeSieve() {
    memset(sieve, false, sizeof sieve);
    sieve[0] = sieve[1] = true;
    for (int i=2; i*i<MX; ++i) {
        if ( ! sieve[i]) {
            for (int j=i*i; j<MX; j+=i) {
                sieve[j] = true;
            }
        }
    }
}

int main() {
    makeSieve();
    int N, P;
    while (cin >> N >> P, N != -1) {
        vector<int> primes;
        for (int i=N+1; (int)primes.size()<P; ++i) {
            if ( ! sieve[i]) {
                primes.push_back(i);
            }
        }
        vector<int> sum;
        for (int i=0; i<(int)primes.size(); ++i) {
            for (int j=i; j<(int)primes.size(); ++j) {
                sum.push_back(primes[i] + primes[j]);
            }
        }
        sort(sum.begin(), sum.end());
        cout << sum[P-1] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
  while(1){
    int n,p;
    vector <int>an;
    int so[101];
    cin>>n>>p;
    if(n==-1&&p==-1)break;
    int c=0;    
    for(int i=n;c<=p;i++){
      int f=0;
      if(i==1||i==0)continue;
      for(int j=2;j*j<=i;j++){
	if(i%j==0){f=1;break;}
      }
      if(f==0){so[c]=i;c++;}
    }
    for(int i=0;i<p;i++)
      for(int j=i;j<p;j++)
	an.push_back(so[i]+so[j]);
    sort(an.begin(),an.end());
    cout<<an[p-1]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<set>
#include<vector>
#define MAX 1000000
using namespace std;
int main(){
	multiset<int> box;
	int q,n,p;
	vector<int> pr,pri;
	for(int i=0;i<=MAX;i++)
	pr.push_back(1);
	pr[0]=pr[1]=0;
	q=0;
	for(int i=0;i<=MAX&&q<=100;i++){
		if(pr[i]){
			pri.push_back(i);
			if(i>=100000)
			q++;
			for(int j=i*2;j<=MAX;j+=i){
				pr[j]=0;
			}
		}
	}
	int now;
	while(cin>>n>>p,n!=-1){
		for(int i=0;i<pri.size();i++)
		if(n<pri[i]){
			now=i;
			break;
		}
		for(int i=now;i<now+p;i++)
		for(int j=i;j<now+p;j++)
		box.insert(pri[i]+pri[j]);
		now=1;
		multiset<int>::iterator ite=box.begin();
		while(now!=p){
			ite++;
			now++;
		}
		cout<<*ite<<endl;
		box.clear();
	}
	return 0;
}