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<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include <vector>
#include<math.h>


using namespace std;

bool f[10000002];

int main(){
	for(int i=3;i<10000001;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++;
		}

		 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);
			}
			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;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 400000;
int for_erat[N];
vector<int> prime;
vector<int> v;
vector<int> res;


void erat(){
    for(int i = 0; i < N; i++){
        for_erat[i] = 1;
    }
    for(int i = 2; i < sqrt(N); i++){
        if(for_erat[i]){
            for(int j = 0; i * (j + 2) < N; j++){
                for_erat[i *(j + 2)] = 0;
            }
        }
    }

    for(int i = 2; i < N; i++){
        if(for_erat[i]){
            prime.push_back(i);
        }
    }
}

int main(){
	erat();

while(1){//start
	//init
	v.clear();
	res.clear();


	int n, p;
	cin >> n >> p;
	if(n == -1 and p == -1) break;

	//n以上の素数を15個見つける
	int idx = 0;
	while((int)v.size() != 50){
		if(prime[idx] > n) v.push_back(prime[idx]);
		idx++;
	}

	for(int i = 0; i < (int)v.size(); i++){
		for(int j = i; j < (int)v.size(); j++) res.push_back(v[i] + v[j]);
	}

	sort(res.begin(), res.end());
	//res.erase(unique(res.begin(), res.end()), res.end());

	cout << res[p-1] << endl;
	//for(int i = 0; i < (int)res.size(); i++) cout << res[i] << endl;


}//end
	return 0;

}

C++: /*
 */
#include <iostream>
#include <algorithm>

using namespace std;

int sosu[9692];
int sosuN;

int iN; // 0 <= N <= 100,000
int iP; // 1 <= P <= 100

int iM[5050];

void init() {
  int i, j;
  bool b[101198];

  sosuN = 0;
  fill(b, b + 101198, true);
  b[0] = false;
  b[1] = false;

  for (i = 2; i < 101198; i++) {
    if (b[i]) {
      sosu[sosuN++] = i;
      for (j = 2; i * j < 101198; j++) {
        b[i * j] = false;
      }
    }
  }
}

bool input() {
  scanf("%d %d", &iN, &iP);
  if (iN == -1 && iP == -1) return false;
  return true;
}

void manage(){
  int i, j, k;
  int cnt = 0;
  int tmp;

  for (i = 0; i < sosuN; i++) {
    if (sosu[i] > iN) break;
  }

  for (j = 0; j < iP; j++) {
    for (k = j; k < iP; k++) {
      iM[cnt++] = sosu[i + j] + sosu[i + k];
    }
  }

  for (j = 0; j < iP; j++) {
    for (k = cnt - 1; k > j; k--) {
      if (iM[k - 1] > iM[k]) {
        tmp = iM[k];
        iM[k] = iM[k - 1];
        iM[k - 1] = tmp;
      }
    }
    //printf("%d\n", iM[j]);
  }
}

void output(){
  printf("%d\n", iM[iP - 1]);
}

int main() {
  init();
  while(1) {
    if (!input()) break;
    manage();
    output();
  }
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const int N = 110000;

int main() {
    int n, p;
    VI primes, d(N,1);
    FOR(i,2,N-1){
        if (!d[i]) continue;
        primes.push_back(i);
        int x = 2*i;
        while (x < N){
            d[x] = 0;
            x += i;
        }
    }
    while (cin >> n >> p && p){
        VI cand;
        int i = 0;
        while (primes[i] <= n) i++;
        FOR(j,i,i+p) FOR(k,i,j){
            cand.push_back(primes[j]+primes[k]);
        }
        sort(cand.begin(), cand.end());
        cout << cand[p-1] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
int main(void){
    int prime[300010]={};
    int b=0;
    for(int i=2;i<=200274;i++){
        if(prime[i]==0){
            prime[i]=1;
            b++;
            for(int j=2*i;j<=200274;j+=i){
                prime[j]=-1;
            }
        }
    }
    vector<int> p(b);
    int a=0;
    for(int i=0;i<200274;i++){
        if(prime[i]==1){
            p[a]=i;
            a++;
        }
    }
    while(1){
        int n,m;
        cin>>n>>m;
        if(n==-1&&m==-1)break;
        long long s[200275]={};
        for(int i=0;i<b;i++){
            for(int j=i;j<b;j++){
                int sum=p[i]+p[j];
                if(sum<=200274&&p[i]>n&&p[j]>n)s[sum]++;
            }
        }
        long long t=0;
        for(int i=4;i<200275;i++){
            t+=s[i];
            if(t>=m){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}