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 <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;
#define MAX 150000


vector<int> sosuu(MAX,1);
vector<int> pnum;

void ini(){
	sosuu[0] = 0;
	sosuu[1] = 0;
	for(int i=2;i<MAX;i++){
		for(int j=2*i;j<MAX;j+=i){
			sosuu[j] = 0;
		}
	}
	for(int i=2;i<MAX;i++){
		if(sosuu[i]) pnum.push_back(i);
	}
	return;
}

bool solve(){
	int n, p;
	cin>> n>> p;
	if(n==-1 && p==-1) return false;
	
	vector<int> a;
	for(int i=0;i<MAX;i++){
		if(pnum[i] > n){
			for(int j=i;j<i+p;j++) a.push_back( pnum[j] );
			break;
		}
	}
	vector<int> ans;
	for(int i=0;i<a.size();i++){
		for(int j=i;j<a.size();j++){
			ans.push_back( a[i] + a[j] );
		}
	}
	sort(ans.begin(), ans.end());
	cout<< ans[p-1]<< endl;
	
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	ini();
	while(solve());

	return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back

typedef long long ll;
typedef pair<int,int> P;

//-----------------------------------------------------------------------

const int SZ=150000;
bool isPrime[SZ];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    for(int i=2;i<SZ;i++) isPrime[i]=true;
    for(int i=2;i<SZ;i++){
	if(!isPrime[i]) continue;
	for(int j=i*2;j<SZ;j+=i){
	    isPrime[j]=false;
	}
    }

    while(true)
    {
	int n,p; cin>>n>>p;
	if(n==-1 && p==-1) break;

	vector<int> v;
	for(int i=n+1;;i++){
	    if(isPrime[i]) v.push_back(i);
	    if(v.size()==p) break;
	}
	vector<int> vv;
	REP(i,v.size()){
	    for(int j=i;j<v.size();j++){
		vv.push_back(v[i]+v[j]);
	    }
	}
	sort(vv.begin(),vv.end());
	cout<<vv[p-1]<<endl;
    }


}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)

using namespace std;

int N, P;
bool flag[102000];
vector<int> v;

int main() {
  REP(i,0,102000) flag[i] = true;
  for(int i = 2; i<102000; i++){
    if(!flag[i]) continue;
    v.push_back(i);
    for(int j = i*2; j<102000; j += i){
      flag[j] = false;
    }
  }

  while(1){
    cin>>N>>P;
    if(N==-1&&P==-1) break;
    int pos;
    REP(i,0,v.size()){
      if(N < v[i]){
        pos = i;
        break;
      }
    }
    vector<int> nums;
    REP(i,pos,v.size()){
      REP(j,i,v.size()){
        int num = v[i]+v[j];
        nums.push_back(num);
      }
    }
    sort(nums.begin(),nums.end());
    cout<<nums[P-1]<<endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    std::deque<long long int> Prime;
    Prime.emplace_back(2);
    for (int i=3;i<=2000000;i+=2) {
        int flag=0;
        double SqrtNum=sqrt(i);
        for (int j=3;j<=SqrtNum;j+=2) {
            if (i%j==0) {
                flag=1;
                break;
            }
        }
        if (flag==0) {
            Prime.emplace_back(i);
        }
    }
    int n,p;
    while (cin>>n>>p) {
        if (n==-1&&p==-1) {
            break;
        }
        std::deque<long long int> sum;
        auto itr=upper_bound(Prime.begin(),Prime.end(),n);
        for (auto itr1=itr;itr1!=itr+100;itr1++) {
            for (auto itr2=itr1;itr2!=itr+100;itr2++) {
                sum.emplace_back(*itr1+*itr2);
            }
        }
        sort(sum.begin(),sum.end());
        cout<<sum[p-1]<<endl;
    }
}

C++: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using ull=unsigned long long int;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
vector<bool> prime;
void make(int num){
    prime.resize(num,true);
    prime[0]=false;
    for(int i=0;i*i<num;i++){
        if(prime[i]){
            for(int j=2;(i+1)*j<=num;j++){
                prime[(i+1)*j-1]=false;
            }
        }
    }
}
int main(){
	make(300000);
	V<ll> dat;
	for(ll i=0;i<300000;i++){
		if(prime[i])dat.emplace_back(i+1);
	}
	while(1){
		ll n,m;
		cin>>n>>m;
		if(n==-1)return 0;
		int ind=upper_bound(all(dat),n)-dat.begin();
		V<ll> tmp;
		int l=0,r=1e6;
		while(r-l>1){
			int mid=(l+r)/2;
			ll cnt=0;
			for(int i=ind;i<dat.size();i++){
				if(mid-dat[i]<dat[i])break;
				int d=upper_bound(all(dat),mid-dat[i])-dat.begin()-i;
				if(d>0)cnt+=d;
				if(cnt>m)break;
			}
			if(cnt>=m)r=mid;
			else l=mid;
		}
		cout<<r<<"\n";
	}
}