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<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

template<int m>
class Prime{
  std::vector<bool> p;
public:
  std::vector<int> ps;
  Prime(){
    p = std::vector<bool>(m,true);
    assert(m>1);
    p[0]=p[1]=false;
    for(int i=4;i<m;i+=2)
      p[i]=false;
    ps.push_back(2);
    for(int i=3;i<m;i+=2){
      if(p[i]){
        ps.push_back(i);
        for(int j=i+i;j<m;j+=i)
          p[j]=false;
      }
    }
  }

  bool isPrime(int n){
    assert(m>n);
    return p[n];
  }

  int operator [] (int n){
    if(n<ps.size()) return ps[n];
    else return 0;
  }

  int size(){
    return ps.size();
  }
};

int main(){
  Prime<100000+10000> p;
  int n,m;
  while(scanf("%d%d",&n,&m), n + m != -2){
    vector<int> v((m+1)*m/2);
    int base = lower_bound(ALL(p.ps), n) - p.ps.begin();
    vector<int>::iterator it = v.begin();
    if(p.ps[base] == n) base++;

    for(int i=0; i<m; i++)
      for(int j=i; j<m; j++)
        *it++ = p.ps[base + i] + p.ps[base + j];

    sort(v.begin(), v.end());
    printf("%d\n",v[m-1]);
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
bool prime[200005];

void init(void){
  for(int i = 0 ; i < 200005 ; i++){prime[i] = true;}
  prime[0] = false, prime[1] = false;
  for(int i = 2 ; i < 200005 ; i++){
    if(prime[i] == true){
      for(int k = 2 ; i * k < 200005 ; k++){
	prime[i * k] = false;
      }
    }
  }
}

int main(void){
    init();
    for(;;){
        int N, P;
        cin >> N >> P;
	if(N == -1 && P == -1){return 0;}
        int p[100];
        int ptr=0;
        for(int i = N + 1 ; ptr<100 ; i++){
            if(prime[i]){p[ptr] = i; ptr++;}
        }
        int sum[50*101]; ptr=0;
        for(int i = 0 ; i < 100 ; i++){
            for(int j = i ; j < 100 ; j++){
                sum[ptr] = p[i] + p[j]; ptr++;
            }
        }
        for(int i = 0 ; i < 5050 ; i++){
            for(int j = i+1 ; j < 5050 ; j++){
                if(sum[i] > sum[j]){
                    int temp = sum[i];
                    sum[i] = sum[j];
                    sum[j] = temp;
                }
            }
        }
        cout << sum[P-1] << endl;
    }
}

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 = 150000;

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<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long int i=0;i<n;++i)
typedef long long int ll;

int main(){

    vector<bool> f(1e+6,true);
    f[1]=false;
    f[0]=false;
    for(int i=2;i<1e+6;i++){
        if(f[i]){
            for(int j=i+i;j<1e+6;j+=i){
                f[j]=false;
            }
        }
    }

    vector<int> s;
    rep(i,1e+6){
        if(f[i]){
            s.push_back(i);
        }
    }
    sort(s.begin(),s.end());
    while(1){
        int n,p;
        cin >> n >> p;
        if(n==-1&&p==-1){
            break;
        }
        vector<int> a;
        int id=distance(s.begin(),lower_bound(s.begin(),s.end(),n));
        if(s[id]==n)id++;
        for(int i=id;i<=id+p;i++){
            for(int j=i;j<=id+p;j++){
                a.push_back(s[i]+s[j]);
            }
        }
        sort(a.begin(),a.end());
        cout << a[p-1] << endl;
    }

    return 0;
}