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;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define repr(i,n) for(int i=(int)(n-1);i>=0;i--)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout << #x" = " << x << endl
#define print(x) cout << x << endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX/3
#define MAX_N 1000000001

bool prime[1000000];
vector<int> v;

void is_prime() {
    memset(prime, 0, sizeof(prime));
    repl(i, 2, 1000000) {
        if (!prime[i]) {
            v.pb(i);
            for(int j = i; j < 1000000; j += i) prime[j] = true;
        }
    }
}

int main(){
    cin.sync_with_stdio(false);
    int n, p;
    is_prime();
    while(cin >> n >> p, n > -1) {
        int id = lower_bound(all(v), n) - v.begin();
        if (v[id] == n) id++;
        vector<int> wa;
        rep(i, p)repl(j, i, p) {
            wa.pb(v[id + i] + v[id + j]);
        }
        sort(all(wa));
        cout << wa[p - 1] << endl;
    }

    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+1000){
					//REP(i,prime.size()) cout << prime[i] << endl;
					cout << matsuzaki[P-1] << endl;
					break;
				}
			}
			N++;
		}
	}
	
}

C++: #include <bits/stdc++.h>
#define MAX 222222
using namespace std;
bool is_prim[MAX];

bool isPrim(int x){
  for(int i=2; i < sqrt(x); i++){
    if(x % i == 0)
      return false;
  }
  return true;
}

void init(void){
  for(int i=0; i < MAX; i++){
    is_prim[i] = true;
  }
  is_prim[0] = false;
  is_prim[1] = false;
  for(int i=2; i < MAX; i++){
    for(int j=i+i; j < MAX; j+=i){
      is_prim[j] = false;
    }
  }
}

int main(void){
  int N, P;
  init();
  while(cin >> N >> P, !(N == -1 && P == -1)){
    list<int> seq;
    //int i = 1;
    //while(P > 0){
    //  if(is_prim[i+N]){
    //    seq.insert(seq.end(), i+N);
    //    i++;
    //    P--;
    //  }else{
    //    i++;
    //  }
    //}
    for(int i=1, cnti=0;; i++){
      if(is_prim[i+N]){
        for(int j=i, cntj=0;; j++){
          if(is_prim[j+N]){
            seq.insert(seq.end(), i+N+j+N);
            if(cntj > P) break;
            cntj++;
          }
        }
        if(cnti > P) break;
        cnti++;
      }
    }
    seq.sort();
    list<int>::iterator it = seq.begin();
    for(int i=1; i < P; i++){
      ++it;
    }
    cout << *it << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int main(){
  int n,p;

  while(cin>>n>>p&&n!=-1&&p!=-1){
    vector<int> prime;

    if(n<=3){
      n=5;
      prime.push_back(2);
      prime.push_back(3);
      prime.push_back(5);
    }
    for(int i=n+1;i<=101000;i++){
      if(prime.size()==200) break;
      for(int j=2;j<=(int)sqrt(i);j++){
        if(i%j==0) break;
        if(j==(int)sqrt(i)){
          prime.push_back(i);
        }
      }
    }

    vector<int> sum;
    for(int i=0;i<prime.size();i++){
      for(int j=i;j<prime.size();j++){
 //       if(n==100000) cout << prime[i] << " + " << prime[j] << endl;
        sum.push_back(prime[i]+prime[j]);    
      }
    }

    sort(sum.begin(), sum.end());
    cout << sum[p-1] << endl;
  }

  return 0;
}

C++: //41
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
  bool cmp[1000000]={true,true};
  vector<int> v;
  for(int i=2;i<1000000;i++){
    if(!cmp[i]){
      v.push_back(i);
      for(int j=2;i*j<1000000;j++){
	cmp[i*j]=true;
      }
    }
  }
  for(int n,p;cin>>n>>p,n!=-1;){
    vector<int> a;
    int x=lower_bound(v.begin(),v.end(),n)-v.begin();
    for(int i=0;i<100;i++){
      for(int j=i;j<100;j++){
	a.push_back(v[x+i]+v[x+j]);
      }
    }
    sort(a.begin(),a.end());
    cout<<a[p]<<endl;
  }
  return 0;
}