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

#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REP(i,n) FOR(i,0,n)
typedef vector<int> vi;
typedef vector<vi> vii;
#define ALL(a) a.begin(),a.end()

int p[5000001]={1,1,0};
vi primes;
vi lp;
void prime(){
	FOR(i,2,5000001){
		if(!p[i]){
			primes.push_back(i);
			for(int j=1;i*j<5000001;j++){
				p[i*j]=1;
			}
		}
	}
}
void listp(int n,int p){
	lp.clear();
	int ii=0,jj=0;
	for(ii=0;primes[ii]<=n;ii++);
	for(jj=0;primes[jj]<=n;jj++);
	for(int i=ii;i<=ii+p;i++){
		for(int j=max(jj,i);j<=ii+p;j++){
			lp.push_back(primes[i]+primes[j]);
		}
	}
	sort(ALL(lp));
}

int main(){
	int n,p;
	prime();
	while(cin>>n>>p,n>-1&&p>-1){
		listp(n,p);
		cout<<lp[p-1]<<endl;
	}
}

C++: 
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2150

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

#define PRIME_MAX 200000

using namespace std;


int matsuzaki(int n, int p, vector<int> &prime_list)
{
	vector<int> m_list;

	int border=0;
	rep(i,prime_list.size()){
		if(prime_list[i] > n){border=i; break; }
	}

	int tmp;
	REP(i,border,border+101)
	{
		tmp = prime_list[i] + prime_list[i];
		m_list.push_back(tmp);
		REP(j,i+1,border+101)
		{
			tmp = prime_list[i] + prime_list[j];
			if(i != j){ m_list.push_back(tmp); }
		}
	}

	sort(ALL(m_list));

	return m_list[p-1];	
}

int main()
{
	int N,P;

	bool is_prime[PRIME_MAX];

	is_prime[0] = false;
	is_prime[1] = false;
	REP(i,2,PRIME_MAX) is_prime[i] = true;
	REP(i,2,PRIME_MAX) {
		if (is_prime[i]) {
				for (int j=i+i; j<PRIME_MAX; j+=i) is_prime[j] = false;
		}
	}

	vector<int> prime_list;
	rep(i,PRIME_MAX){ 
		if(is_prime[i]){ prime_list.push_back(i);}
	}

	while(cin >> N >> P)
	{
		if(N == -1 && P == -1){ break; }
		cout << matsuzaki(N,P,prime_list) << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9:1e18)
#define EPS 0.0000000001
using namespace std;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
template<class T> void join(T a){int b=0;for(auto itr :a){if(b++!=0)cout << " "; cout << itr;} }
using ll  = long long;
using ld  = long double;
using pii = pair<int,int>;
using piii = pair<int,pii>;
int W,H;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
bool valid(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
#define int ll
class Prime{
public:
    vector<bool>Isprime;
    vector<int>prime;
    int size;
    Prime(int n) : Isprime(n+1,true),size(n+1){
        Isprime[0] = Isprime[1] = false;
        for(int i = 2; i<=size; i++){
            if(!Isprime[i])continue;
            prime.push_back(i);
            for(int j = 2;i*j <=size;j++) Isprime[i*j] = false;
        }
    }
    bool IsPrime(int n){return (n<=size? Isprime[n]:false);}
    int indexOf(int i){return (i<prime.size()?prime[i]:INF);}
};

signed main(){
    int n,p;
    Prime pr(10000000);
    while(cin >> n >> p){
        if(n == -1 && p == -1)break;
        vector<int>v;
        rep(i,pr.prime.size()){
            if(pr.prime[i] > n)v.push_back(pr.prime[i]);
            if(v.size()>300)break;
        }
        vector<int>hoge;
        rep(i,v.size()){
            for(int j = i; j < v.size();j++){
                hoge.push_back(v[i]+v[j]);
            }
        }
        sort(all(hoge));
        cout << hoge[--p] << endl;
    }
}

C++: include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
#include<list>
#include<set>
using namespace std;
#define fr first
#define sc second
#define INFTY 1000000
typedef pair< int,int > iiP;
typedef pair< int,iiP > P;
int main(){
  int N,P;
  bool Prime[INFTY + 1]={0};
  vector<int> Matsuzaki;
  vector<int> Prime_No;
  for(int i=2;i<INFTY;i++){
    if(!Prime[i]){
      Prime_No.push_back(i);
      for(int j=2;i*j<INFTY;j++) Prime[i*j] = true;
    }
  } 
  while(cin >> N >> P) {
    if(N == -1 && P == -1) break;
    int s =  upper_bound(Prime_No.begin(),Prime_No.end(),N) - Prime_No.begin();
    for(int i=0;i<=100;i++){
      for(int j=i;j<=i + 100;j++){
	Matsuzaki.push_back(Prime_No[s + i] + Prime_No[s + j]);
      }
    }
    sort(Matsuzaki.begin(),Matsuzaki.end());
    cout << Matsuzaki[P - 1] << endl;
    Matsuzaki.clear();
  }
}