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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2150&lang=jp>
 問題文============================================================
 =================================================================
 解説=============================================================
 あらかじめ素数を列挙しておき、探索する
 ================================================================
 */
const ll MAX_PRIME = 200000;
vector<int> primes;
vector<int> is_prime(MAX_PRIME + 1,true);
void init_primes(){
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i <= MAX_PRIME;i++){
        if(is_prime[i]){
            primes.push_back(i);
            for(int j = i*2;j<=MAX_PRIME;j+=i) is_prime[j] = false;
        }
    }
}

void solve(){
    init_primes();
    ll N,P;
    vector<ll> sum;
    while(cin >> N >> P){
        if(N == -1 && P == -1) break;
        sum.clear();
        auto idx = upper_bound(primes.begin(),primes.end(),N) - primes.begin();
        for(int i = 0; i <= P;i++){
            for(int j = i; j <= P;j++){
                ll S = primes[idx+i] + primes[idx+j];
                sum.push_back(S);
            }
        }
        sort(sum.begin(),sum.end());
        cout << sum[P-1] << endl;
    }
}
int main(void){
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve();
    return 0;
}

C++: #include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;

class Solver {
public:
  Solver();
  void solve(int N, int P);
private:
  bool isprime[200000];
};

Solver::Solver() {
  for(int i = 0; i < 200000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for(int i = 2; i < 2000; ++i) {
    if( isprime[i] ) {
      for(int j = i * 2; j < 200000; j+=i) {
        isprime[j] = false;
      }
    }
  }
}

void Solver::solve(int N, int P) {
  std::vector<int> can;
  for(int i = N + 1; i < std::min(N + P * 3 + 10, 200000); ++i) {
    if( not isprime[i] ) continue;
    for(int j = i; j < std::min(N + P * 3 + 10, 200000); ++j) {
      if( not isprime[j] ) continue;
      can.push_back(i + j);
    }
  }
  std::sort(std::begin(can), std::end(can));
  std::cout << can[P-1] << std::endl;
}


int main() {

  Solver solver;
  
  for(;;) {
    int N, P;
    std::cin >> N >> P;
    if( N == 0 and P == 0 ) break;
    
    solver.solve(N, P);
  }  
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
using ll=long long;
#define int ll
 
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
 
int read(){
    int i;
    scanf("%lld",&i);
    return i;
}
 
bool is_prime(int n){
    if(n==0||n==1) return false;
    if(n==2) return true;
    for(int i=2;i<n;i++){
        if(n%i==0) return false;
    }
    return true;
}
 
signed main(){
    while(true){
        int N=read(),P=read();
        if(N==-1&&P==-1) break;
        vector<int> prime;
        vector<int> matsuzaki;
        while(1){
            if(is_prime(N)){
                prime.push_back(N);
                REP(j,prime.size()){
                    matsuzaki.push_back(prime[j]+N);
                }
                sort(matsuzaki.begin(),matsuzaki.end());
                if(prime.size()>=P+100){
                    //REP(i,prime.size()) cout << prime[i] << endl;
                    cout << matsuzaki[P-1] << endl;
                    break;
                }
            }
            N++;
        }
    }
     
}