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<iomanip>
#include<algorithm>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

typedef long long ll;
typedef unsigned long long ull;

//?´???°??????
bool isPrime(int x) {
	if (x < 2)return 0;
	else if (x == 2)return 1;
	if (x % 2 == 0)return 0;
	for (int i = 3; i*i <= x; i += 2)
		if (x%i == 0)return 0;
	return 1;
}

//??¨????????????????????????
//n??\????????£??´??°????´???°???????????°???????????????is_prime[]???????´?
void eratos(int n, bool is_prime[]) {
	fill(is_prime, is_prime + n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i*i <= n; i++) {
		if (is_prime[i]) {
			int j = i + i;
			while (j <= n) {
				is_prime[j] = false;
				j = j + i;
			}
		}
	}
}

//?????§??¬?´???°
int gcd(int x, int y) {
	return y ? gcd(y, x%y) : x;
}

//???????´???? ??°??????????????????
ll mod_pow(ll x, ll n) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = res*x%MOD;
		x = x*x%MOD;
		n >>= 1;
	}
	return res;
}

static const int MAX_N = 100000;
static const int MAX_P = 100;

int main() {
	bool is_prime[MAX_N * 2];
	eratos(MAX_N * 2, is_prime);
	vector<int> prime;
	for (int i = 0; i <= MAX_N * 2; i++) {
		if (is_prime[i]) {
			prime.push_back(i);
		}
	}

	static vector<int> M[MAX_N + 1];
	for (int N, P; cin >> N >> P&&N != -1;) {
		if (M[N].size() == 0) {
			auto mprime = lower_bound(prime.begin(), prime.end(), N) - prime.begin();
			if (is_prime[N])mprime++;
			for (auto i = mprime; i < mprime + MAX_P; i++) {
				for (int j = i; j < mprime + MAX_P; j++) {
					M[N].push_back(prime[i] + prime[j]);
					//cout << prime[i] << " " << prime[j] << " " << prime[i] + prime[j] << endl;
				}
			}
			sort(M[N].begin(), M[N].end());
		}
		cout << M[N][P - 1] << endl;
	}
	return 0;
}

C++: // 2016-12-12
#include <iostream>
#include <algorithm>
#include <cmath>
#include <array>
#include <map>
#include <vector>
using namespace std;

int main() {
	int n, p;
	bool a[100275];
	fill(a, a + 100275, true);
	a[0] = a[1] = false;
	vector<int> v;
	for (int i = 2; i < 100275; i++) {
		if (a[i]) {
			v.push_back(i);
			for (int j = i + i; j < 100275; j += i) {
				a[j] = false;
			}
		}
	}
	vector<pair<int, int>> w;
	for (int i = 0; i < v.size(); i++) {
		for (int j = i, l = min((int)v.size(), i + 50); j < l; j++) {
			if (v[i] + v[j] > 200274) {
				break;
			} else {
				w.push_back(make_pair(v[i] + v[j], min(v[i], v[j])));
			}
		}
	}
	sort(begin(w), end(w));
	while (cin >> n >> p, n >= 0) {
		int i, m;
		for (i = 0, m = 0; i < w.size(); i++) {
			if (w[i].second > n) {
				m++;
				if (m >= p) {
					break;
				}
			}
		}
		cout << w[i].first << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int a[100000]={2,3},i,j,k,c=2,b[100002][101],q,w[450];
int main(){
  for(i=4;i<150000;i++){
    for(k=1,j=2;j*j<=i;j++){
      if(i%j==0){k=0;break;}
    }
    if(k)a[c++]=i;
  }k=0;
  for(i=0;i<=100000;i++){
    if(i>=a[k])k++;
    for(q=0,j=k;j<k+20;j++){
      for(c=j;c<j+20;c++){
        w[q++]=a[j]+a[c];
      }
    }
    sort(w,w+q-1);
    for(c=0;c<=100;c++)b[i][c]=w[c];
  }
  while(cin>>i>>j){
    if(i<0)break;
    cout<<b[i][j-1]<<endl;
  }
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>
#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
using namespace std;
vector<bool> era(1000010, true);
void makePrimeTable(int n)
{
	era[0] = false; era[1] = false;
	for (int i = 2; i <= n; i++)
	{
		if (era[i]) { for (int j = i * 2; j <= n; j += i) { era[j] = 0; } }
	}
}
int main()
{
	makePrimeTable(200000);
	int N, P; while (cin >> N >> P && N != -1)
	{
		vector<int> dat(0);
		vector<int> prm(0);
		if (P > 50) { P /= P / 10; }
		while (1)
		{
			N++;
			if (era[N]) { prm.push_back(N);}
			if (prm.size() == P) { break; }
		}
		for (int i = 0; i < P; i++) for (int j = i; j < P; j++) { dat.push_back(prm[i] + prm[j]); }
		printf("%d\n", dat.size());
		sort(all(dat)); 
		//printf("%d\n", dat[P - 1]);
	}
	return 0;
}