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<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==22)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++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void findPrime(int N, vector<bool>& isPrime)
{
    isPrime.assign(N+1, true);
    isPrime[0] = isPrime[1] = false;
    for(int i=2; i*i<=N; i++){
        if(isPrime[i]){
            for(int j=i; i*j<=N; j++){
                isPrime[i*j] = false;
            }
        }
    }
}

int main()
{
    vector<bool> prime;
    findPrime(300000, prime);

    for(;;){
        int n, p;
        cin >> n >> p;
        if(n == -1)
            return 0;

        vector<int> a;
        for(int i=n+1; ; ++i){
            if(prime[i]){
                a.push_back(i);
                if(a.size() == 20)
                    break;
            }
        }

        vector<int> b;
        for(int i=0; i<20; ++i){
            for(int j=i; j<20; ++j){
                b.push_back(a[i] + a[j]);
            }
        }
        sort(b.begin(), b.end());

        cout << b[p-1] << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1e5;

int prime[MAX_N];
bool is_prime[MAX_N*2];

int siege(int n) {
    int p = 0;
    fill(is_prime, is_prime+n, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            prime[p++] = i;
            for (int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
    return p;
}

int main(void) {
    int p = siege(MAX_N*2);
    int N, P;
    while (cin >> N >> P) {
        if (N == -1 && P == -1) break;

        int t = 0;
        int func_M[1000];
        int st = upper_bound(prime, prime+p, N) - prime;
        for (int i = st; i < st+40; i++) {
            for (int j = i; j < st+40; j++) {
                func_M[t++] = prime[i]+prime[j];
            }
        }
        sort(func_M, func_M+t);
        cout << func_M[P-1] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int isPrime(int n){
  for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
  return 1;
}
int main(){
  int n,p,i,j,k,o,dp[200000]={};
  for(i=2;i<200000;i++) dp[i]=isPrime(i);
  cin >> n >> p;
  while(n!=-1){
    int c=0;
    int l[100]={};
    i=n;
    while(c<100){
      i++;
      if(dp[i]==1){
	l[c]=i;
	c++;
      }
    }
    vector <int> v;
    for(i=0;i<100;i++){
      for(j=i;j<100;j++){
	v.push_back(l[i]+l[j]);
      }
    }
    sort(v.begin(),v.end());
    cout << v[p-1] << endl;
    cin >> n >> p;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

int const N = 100000+10000;
bool isp[N];
int ps[N / 2];
int c = 0;
void eratos(){
    rep(i, N) isp[i] = true;
    isp[0] = isp[1] = false;
    for (int i = 2; i < N; i++){
        if (!isp[i]) continue;
        ps[c++] = i;
        for (int j = i + i; j < N; j += i){
            isp[j] = false;
        }
    }
}

int solve(int n, int p){
    auto it = upper_bound(ps, ps + c, n);
    vi v(p*p, 1 << 30);
    rep(i, p)loop(j, i, p){
        v[i * p + j] = *(it + i) + *(it + j);
    }
    sort(all(v));
    return v[p - 1];
}

int main(){
    eratos();
    int n, p;
    while (cin >> n >> p && n!=-1 && p!=-1){
        cout << solve(n, p) << endl;
    }
}