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

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int,int> PI;
#define EPS (1e-10L)
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(),a.end()
#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)
void pkuassert(bool t){t=1/t;};

int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};


bool ispr(int n){
  if(n==1) return false;
  for(int i=2;i*i<=n;++i)
    if(n%i==0) return false;
  return true;
}

int main(int argc, char *argv[])
{
  int n,p;
  while(cin >> n >> p && ~n){
    vector<int> app;
    while(SZ(app)<=p){
      ++n;
      if(ispr(n)) app.pb(n);

    }
    vector<int> wa;
    rep(i,SZ(app)) for(int j=i;j<SZ(app);++j) wa.pb(app[i]+app[j]);
    sort(ALL(wa));
    cout << wa[p-1] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_N = 2000000;

bitset<MAX_N> sieve;
vector<int> primes;
void eratosthenes(){
	sieve[0] = sieve[1] = true;
	for(int i = 2; i < MAX_N; ++i){
		if(sieve[i]){ continue; }
		primes.push_back(i);
		for(int j = i + i; j < MAX_N; j += i){ sieve[j] = true; }
	}
}

int main(){
	eratosthenes();
	while(true){
		int n, p;
		cin >> n >> p;
		int begin =
			upper_bound(primes.begin(), primes.end(), n) - primes.begin();
		vector<int> pp;
		for(int i = 0; i < 200; ++i){
			for(int j = 0; j <= i; ++j){
				pp.push_back(primes[begin + i] + primes[begin + j]);
			}
		}
		sort(pp.begin(), pp.end());
		cout << pp[p - 1] << endl;
	}
	return 0;
}

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

int main() {
bool a[1300000]={0};
a[1]=1;
for(int i=2;i<650001;i++){
	if(a[i]==1) continue;
	int j=2*i;
	while(j<1300000){
		a[j]=1;
		j=j+i;
	}
}

int b[1400000]={0};
int q=1;
for(int i=0;i<1300000;i++){
	q++;
	while(a[q]==1) q++;	

	 b[i]=q;
	
}
//cout<<b[10]<<endl;
int n,p;
while(cin>>n>>p && n>-1){
	int k=0;
	while(b[k]<=n) k++;
	vector<int> v;
//	cout<<k<<endl;
	for(int ii=k;ii<k+500;ii++){
		for(int j=k;j<=ii;j++){
			v.push_back(b[ii]+b[j]);
	//		cout<<ii<<" "<<j<<endl;
			}	
	}
sort(v.begin(),v.end());
//for(int i=0;i<40;i++) cout<<v[i]<<endl;
	cout<<v[p-1]<<endl;
	
}



	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

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

int N, P;
vector<int> p(1, 2);
const int kP = 1000002;
bool sieve[kP];

int main() {
  fill(sieve, sieve + kP, true);
  for (int i = 4; i < kP; i+=2) sieve[i] = false;
  for (int i = 3; i < kP; i+=2) {
    if (sieve[i]) {
      p.push_back(i);
      for (int j = i+i; j < kP; j+=i)
        sieve[j] = false;
    }
  }

  // rep(i,10) printf("{%d}", p[i]); puts("");
  
  while (scanf("%d%d", &N, &P), N != -1) {
    vector<int>::iterator bgn = upper_bound(p.begin(), p.end(), N);
    vector<int> subP;
    rep(i,100) {
      subP.push_back(*(bgn++));
    }
    vector<int> rs;
    rep(i,100) {
      for (int j = i; j < 100; ++j)
        rs.push_back(subP[i]+subP[j]);
    }
    sort(rs.begin(), rs.end());
    // rep(i,10) printf("{%d}", rs[i]); puts("");
    printf("%d\n", rs[P-1]);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define pb push_back

bool is_prime(int n) {
    if(n < 2) return false;
    else if(n == 2) return true;
 
    if(n % 2 == 0) return 0;
    for(int i=3; i*i<=n; i+=2) if(n % i == 0) return false;
    return true;
}

int M(int n, int p) {
    vector<int> primes;
    FOR(i,n+1,n+500) if(is_prime(i)) primes.pb(i);
    vector<int> sums;
    rep(i,SZ(primes)) FOR(j,i,SZ(primes)) sums.pb(primes[i] + primes[j]);
    sort(ALL(sums));
    return sums[p-1];
}

int main() {
    int n,p;
    while(cin>>n>>p) {
        if(n==-1 and p==-1) break;
        cout << M(n,p) << "\n";
    }
    return 0;
}