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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

char bits[111111];
LL primes[10000], n_primes; 

void sieve(){
	int i, j;
	memset(bits, 1, 111111);
	bits[0] = bits[1] = 0;
	for (i = 0; i < 334; i++)
		if (bits[i])
			for (j = i * i; j < 111111; j += i)
				bits[j] = 0;
	for (i = j = 0; i < 111111; i++)	
		if (bits[i]) primes[j++] = i;
	n_primes = j;
}

int main(){
	sieve();
	while(1){
		int n,p;
		cin>>n>>p;
		if(n==-1)return 0;
		int idx=0;
		while(1){
			if(primes[idx]>n)break;
			idx++;
		}
		VI a(2500);
		REP(i,50){
			REP(j,50){
				a[i*50+j]=primes[idx+i]+primes[idx+i+j];
			}
		}
		SORT(a);
		cout << a[p-1] << endl;
	}
}

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

bool seive[200001];
vector<int> matuzakiList;

void init(){
  for(int i = 0; i <= 200000; i++) seive[i] = true;
  seive[0] = seive[1] = false;
  for(int i = 2; i * i <= 200000; i++){
    for(int j = i * i; j <= 200000; j = j + i) seive[j] = false;
  }
}

int matuzakiNumber(int N, int P){
  int prime[20] = {0}, cnt = 0;
  for(int i = N + 1; cnt != 20; i++){
    if(seive[i]) prime[cnt++] = i;
  }
  for(int i = 0; i < 20; i++){
    for(int j = i; j < 20; j++){
      int ml = prime[i] + prime[j];
      matuzakiList.push_back(ml);
    }
  }
  sort(matuzakiList.begin(), matuzakiList.end());
  return matuzakiList[P-1];
}

int main(void){
  init();
  int N, P;
  while(cin >> N >> P && N != -1 && P != -1){
    matuzakiList.clear();
    cout << matuzakiNumber(N, P) << endl;
  }
  return 0;
}

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

vector<int> Prime(100000),Matsuzaki(1000);

int judge(int n){
  if(n<2)return 0;
  if(n==2)return 1;
  if(n%2==0)return 0;
  for(int i=3;i<=n/i;i+=2){
    if(n%i==0)return 0;
  }
  return 1;
}

int main()
{
  int a=0,b=0,N,P,ii;
  for(int i=2;i<1000000;i++){
    if(judge(i))Prime[b++]=i;
  }
  while(1){
    cin>>N>>P;
    if(N==-1&&P==-1)break;
    a=0; 
    for(int i=0;i<b;i++){
      if(Prime[i]>N){
	ii=i;
	break;
      }
    }
    for(int i=ii;i<ii+100;i++){
      for(int j=i;j<ii+100;j++){
	Matsuzaki[a]=Prime[i]+Prime[j];
	a++;
      }
    }
    sort(Matsuzaki.begin(),Matsuzaki.begin()+a);
    cout<<Matsuzaki[P-1]<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;



int main(){
  int s[150000]={};
  s[0]=2;
  int count=1;
  for(int i=3;i<150000;i+=2){
    bool j=true;
    for(int t=0;s[t]*s[t]<=i;t++){
      if(s[t]==0){break;}
      if(i%s[t]==0){j=false; break;}
    }
    if(j){s[count]=i; count++;}
  }


  while(1){

  int n,p;
  cin>>n>>p;
  if(n==-1 && p==-1){break;}
  for(int i=0;i<150000;i++){
    if(s[i]>n){count=i; break;}
  }
  vector<int> a(0);
  for(int i=count;i<=count+p;i++){
    for(int t=i;t<=count+p;t++){
      a.push_back(s[i]+s[t]);
    }
  }
  sort(a.begin(),a.end());
  
  cout<<a[p-1]<<endl;

  }
  

  
  
  return 0;
}

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

vector<int> sieve_of_eratosthenes(int n) {
	vector<int> primes(n);
	for (int i = 2; i < n; ++i)
		primes[i] = i;
	for (int i = 2; i*i < n; ++i)
		if (primes[i])
			for (int j = i*i; j < n; j+=i)
				primes[j] = 0;

//	primes.erase(remove( primes.begin(), primes.end(),0 ), primes.end() );
	return primes;
}

int main()
{
	vector<int> prime=sieve_of_eratosthenes(200000);
	int N,P;
	while(cin>>N>>P && N>=0){
		vector<int> use;
		for(int i=N+1;i<prime.size();i++){
			if(prime[i]!=0) use.push_back(i);
			if(use.size()>100) break;
		}

		vector<int> L;
		for(int i=0;i<=P;i++){
			for(int j=i;j<=P;j++){
				L.push_back(use[i]+use[j]);
			}
		}
		
		sort(L.begin(),L.end());
		cout<<L[P-1]<<endl;
	}
	return 0;
}