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>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define all(x) x.begin(),x.end()
using namespace std;
vector<int>v; int n, k;
signed main() {
	v.push_back(2);
	for (int i = 3; i <= 101197; i += 2) {
		bool b = 1;
		for (int p : v)if (i % p == 0)b = 0; else if (p * p > i)break;
		if (b) v.push_back(i);
	}
	while (cin >> n >> k, k >= 0) {
		auto itr = upper_bound(all(v), n);
		vector<int>v1, v2;
		for0(i, k) v1.push_back(*itr++);
		for0(i, k)for0(j, i + 1)v2.push_back(v1[i] + v1[j]);
		sort(all(v2));
		cout << v2[k - 1] << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 1000000
bool isP[MAX];
vector<int> primes;
void make_table(){
  for(int i=0;i<MAX;i++)isP[i]=true;
  isP[0]=isP[1]=false;
  for(int i=2;i*i<MAX;i++){
    if(!isP[i])continue;
    for(int j=i*i;j<MAX;j+=i)isP[j]=false;
  }
  for(int i=0;i<MAX;i++)
    if(isP[i])primes.push_back(i);
}

int N,P;
int main(){
  make_table();
  vector<int> :: iterator it;
  while(1){
    cin>>N>>P;
    if(N==-1&&P==-1)break;
    it=lower_bound(primes.begin(),primes.end(),N);
    //cout<<*it<<endl;
    vector<int> t,u;
    for(int i=0;i<100;i++,it++){
      t.push_back(*it);
      for(int j=0;j<=i;j++)u.push_back(t[j]+t[i]);
    }
    sort(u.begin(),u.end());
    cout<<u[P-1]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
	bool is(int x){
		for(int i=2;i*i<=x;i++)if(x%i==0)return false;
		return true;
	}
int main(){
	int n,p;

	while(cin>>n>>p,n!=-1||p!=-1){
		int cnt=0;
		n++;
		if(n==1)n++;
		vector<int>primes;
		vector<int>sums;
		while(true){
			if(is(n)){
				cnt++;
				primes.push_back(n);
			}
			if(cnt==78)break;
			n++;
		}
		//for(int i=0;i<primes.size();i++)cout<<primes[i]<<endl;
		for(int i=0;i<primes.size();i++){
			for(int j=0;j<primes.size();j++){
				if(j>primes.size())break;
				if(i<j)break;
				sums.push_back(primes[i]+primes[j]);
			}
		}
		sort(sums.begin(),sums.end());
		cout<<sums[p-1]<<endl;
	}
	return 0;
}

C++: //Name: Matsuzaki Number
//Level: 2
//Category: 素数
//Note:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_PRIME = 200000;
vector<int> primes;
bool is_prime[MAX_PRIME+1];

void init() {
    fill_n(is_prime, MAX_PRIME+1, true);
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i <= MAX_PRIME; ++i) {
        if(is_prime[i]) {
            primes.push_back(i);
            for(long long j = (long long)i*i; j <= MAX_PRIME; j += i) {
                is_prime[j] = false;
            }
        }
    }
}

bool solve() {
    int N, P;
    cin >> N >> P;
    if(N == -1 && P == -1) return false;

    vector<int>::iterator first = upper_bound(primes.begin(), primes.end(), N);
    vector<int> nums;
    const int start = distance(primes.begin(), first);
    for(int i = start; i < start+100; ++i) {
        for(int j = i; j < start+100; ++j) {
            nums.push_back(primes[i]+primes[j]);
        }
    }
    sort(nums.begin(), nums.end());
    cout << nums[P-1] << endl;
    return true;
}

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

    init();
    for(int case_num = 1; solve(); ++case_num) ;
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define MAX 200000

// sieve of Eratosthenes (prime num)
bool p[MAX+1]; //prime number=true
void prime(int n){
  rep(i,n+1) p[i]=true;
  p[0]=p[1]=false;
  for(int i=2;i<=n;i++){
    if(p[i]){
      for(int j=i*2; j<=n; j+=i){
	p[j]=false;
      }
    }
  }
}


int main(){
  prime(MAX);
  int n,P;
  while(scanf("%d %d", &n, &P), (n!=-1)||(P!=-1)){
    int nums[100];
    vector<int> adds;

    // take P prime number bigger than n
    int cnt=0, idx=n+1;
    while(cnt<P){
      if(p[idx]){
	nums[cnt]=idx;
	cnt++;
	rep(i,cnt){
	  adds.pb(nums[i]+idx);
	}
      }
      idx++;
    }

    sort(all(adds));

    cout << adds[P-1] << endl;

  }
  return 0;
}