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<vector>
#include<algorithm>
using namespace std;

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

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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define MAXP 1e5+300

int main(){
    while(true){
        int n,p;
        cin >> n >> p;
        if(n==-1)break;
        vector<bool> table(MAXP,true);
        table[0]=table[1]=false;
        int count=0;
        for(int i=2;i<MAXP;i++){
            for(int j=2;i*j<MAXP;j++){
                table[i*j]=false;
            }
        }
        vector<int> prime(MAXP,MAXP);
        for(int i=0;i<MAXP;i++){
           if(table[i]){
            prime[count]=i;
            count++;
           }
        }
        int ps;
        vector<int> list;
        for(int i=0;i<count;i++){
            if(prime[i]>n){
                ps=i;
                break;
            }
        }
        for(int i=ps;i<ps+30;i++){
            for(int j=ps;j<=i;j++){
               
                list.push_back(prime[i]+prime[j]);

            }
        }
        sort(list.begin(),list.end());
        cout << list[p-1] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
	bool is(int x){
		for(int i=2;i*i<=x;i++)if(x%i==0)return false;
		return true;
	}
int main(){
	int n,p;

	while(cin>>n>>p,n!=-1||p!=-1){
		int cnt=0;
		n++;
		if(n==1)n++;
		vector<int>primes;
		vector<int>sums;
		while(true){
			if(is(n)){
				cnt++;
				primes.push_back(n);
			}
			if(cnt==21)break;
			n++;
		}
		//for(int i=0;i<primes.size();i++)cout<<primes[i]<<endl;
		for(int i=0;i<primes.size();i++){
			for(int j=0;j<primes.size();j++){
				if(j>primes.size())break;
				if(i<j)break;
				sums.push_back(primes[i]+primes[j]);
			}
		}
		sort(sums.begin(),sums.end());
		cout<<sums[p-1]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define MAX_N 200000
using namespace std;
vector<int> ps;
int prime[MAX_N];
int main(){
	for(int i=2;i<MAX_N;i++)prime[i]=1;
	for(int i=2;i<MAX_N;i++){
		if(prime[i]){
			for(int j=2;i*j<MAX_N;j++){
				prime[i*j]=0;
			}
		}
	}
	for(int i=2;i<MAX_N;i++){
		if(prime[i])ps.push_back(i);
	}
	int n,p;
	while(cin>>n>>p&&n>=0&&p>=0){
		vector<int> v;
		vector<int> ::iterator x;
		x=upper_bound(ps.begin(),ps.end(),n);
		for(int i=0;i<=p;i++){
			for(int j=i;j<=p;j++){
				v.push_back((*(x+i))+(*(x+j)));
			}
		}
		sort(v.begin(),v.end());
		cout<<v[p-1]<<endl;
	}
}

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

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

int main() {
  for(int N, P; cin >> N >> P && (~N|~P);) {
    vector<int> v;
    for(int x = N+1; v.size() < P; ++x) {
      if(isPrime(x)) v.push_back(x);
    }
    vector<int> u;
    for(int i = 0; i < v.size(); ++i) {
      for(int j = i; j < v.size(); ++j) {
        u.push_back(v[i] + v[j]);
      }
    }
    sort(u.begin(), u.end());
    cout << u[P-1] << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include <vector>
#include<math.h>


using namespace std;

bool f[1000002];

int main(){
	for(int i=3;i<1000001;i+=2){f[i]=true;
		for(int j=3;j*j<=i;j+=2)
			if(i%j==0)f[i]=false;
	}
	f[2]=true;
	int n,p;
	while(1){
		cin>>n>>p;if(n==-1&&n==p)break;

		int r=0;
		int q[10001]={};
		int x=0;
		int y=n;
		while(1){
			if(f[y]==true)x++;
			if(x==p)break;
			y++;
		}
		n++;
		 vector<int> anslist;
		 for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
					anslist.push_back(i+j);
				
				cout<<i<<" "<<j<<endl;
				}
			}
			sort(anslist.begin(),anslist.end());
/*
		for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
				q[r]=i+j;r++;
				}
			}

		/*	
		sort(q,q+r);
		for(int i=0;i<r;i++)
		cout<<q[i]<<endl;

		cout<<q[p-1]<<endl;*/
			  cout << anslist[p-1] << endl;

	}
	
	return 0;
}