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 <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <math.h>
#include <list>
#include <algorithm>

using namespace std;

#define max 110000


int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	//素数テーブル作成
	vector<bool> v;
	for(int i = 0; i < max; i++)
		v.push_back(true);

	v.at(0) = false;
	v.at(1) = false;
	for(int i = 2; i < max; i++) {
		if( v.at(i) == false ) continue;
		for(int j = 2 * i; j < max; j += i )
			v.at(j) = false;
	}


	while( 1 ) {
		int N, P;
		cin >> N >> P;
		if( N == -1 && P == -1 ) break;

		vector<int> plist;
		for(int i = N + 1; plist.size()*(plist.size() - 1) / 10 < P  && i < max ; i++)
			if( v.at(i) == true ) plist.push_back(i);
		
		vector<int> sumlist;

		for(int i = 0; i < plist.size(); i++) {
			sumlist.push_back( 2 * plist.at(i) );
			for(int j = i + 1; j < plist.size(); j++)
				sumlist.push_back( plist.at(i) + plist.at(j) );
		}

		sort( sumlist.begin(), sumlist.end() );
		
		cout << sumlist.at(P-1) << endl;

	}

	//while(1){}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
const int MAXN = 2*1e5;
vector<int> pri(2*(int)1e5,true);

void prim(){
	pri[1] = false;
	for(int i=2;i*i<MAXN;i++){
		if(pri[i])
			for(int j=2*i;j<MAXN;j+=i)
				if(pri[j])pri[j] = false;
	}
}
int main(){
	int n,p;
	prim();
	while(cin >>n>>p,n!=-1){
		vector<int> twop;
		int limitt = n+p*100;
		for(int i=n+1;i<limitt;i++){
			if(pri[i])
				for(int j=i;j<limitt;j++){
					if(pri[j])twop.push_back(i+j);
				}
		}
		sort(twop.begin(),twop.end());
		cout << twop[p-1] << endl;
	}
	return 0;
}

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

const int MAX_N = 110000;

vector<int> prime_list() {
    char f[MAX_N+1];
    fill(f, f + MAX_N + 1, -1);
    f[0] = f[1] = false;
    for(int i = 2; i <= MAX_N; i++)
        if(f[i])
            for(int j = 2 * i; j <= MAX_N; j += i)
                f[j] = false;
    vector<int> res;
    for(int i = 2; i <= MAX_N; i++) if(f[i]) res.push_back(i);
    return res;
}

int main() {
    vector<int> prime = prime_list();
    for(int N, P; cin >> N >> P, N + P != -2; ) {
        vector<int> li;
        int s = upper_bound(prime.begin(), prime.end(), N) - prime.begin();
        for(int i = s; i <= s + P; i++)
            for(int j = i; j <= s + P; j++)
                li.push_back(prime[i]+prime[j]);
        sort(li.begin(), li.end());
        cout << li[P - 1] << endl;
    }
}

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

using namespace std;

vector<int> MakePrimeNumber(int);
vector<int> MakePlus(int, int, vector<int>);

int main(){
  int n, m, count;
  vector<int> prime, data;

  prime = MakePrimeNumber(200000);

  while(1){
    cin >> n >> m;
    if(n == -1 && m == -1) break;

    data = MakePlus(n, m, prime);

    cout << data[m-1] << endl;
    data.clear();
  }

  return 0;
}

vector<int> MakePrimeNumber(int n){
  int i, j, k;
  vector<int> data;

  data.push_back(2);
  data.push_back(3);
  for(i=5; i<=n; i+=2){
    for(j=1, k=0; data[j]<=sqrt(i); ++j){
      if(i%data[j] == 0){
        k = 1;
        break;
      }
    }
    if(k == 0) data.push_back(i);
  }
  return data;
}

vector<int> MakePlus(int n, int m, vector<int> prime){
  int x;
  vector<int>::iterator i, j, k, l;
  vector<int> data;

  i = upper_bound(prime.begin(), prime.end(), n);
  for(j=i; data.size()<m; ++j){
    for(k=i; k<=j; ++k) data.push_back(*j + *k);
  }

  sort(data.begin(), data.end());

  while(1){
    for(k=i; k<=j; ++k){
      x = *j + *k;
      if(x < data[m-1]){
	l = upper_bound(data.begin(), data.end(), x);
	data.insert(l, x);
      }else if(k == i){
	return data;
      }
    }
    ++j;
  }
}

C++: #include<iostream>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

vector<int> prime;

bool make()
{
  bool tmp[102000];
  memset(tmp,true,sizeof(tmp));
  for(int i=2;i<102000;i++)
    {
      if(tmp[i])
	{
	  prime.push_back(i);
	  for(int j=i+i;j<102000;j+=i)
	    tmp[j]=false;
	}
    }
}

int main()
{
  int n,p;
  make();
  for(;cin>>n>>p,n!=-1;)
    {
      vector<int > data;
      data.clear();
      int i;
      int size=prime.size();
      //      cout<<size<<endl;
      for(i=0;i<size;i++)
	{
	  //	  cout<<prime[i]<<" "<<n<<endl;
	  if(prime[i]>n)
	    {	    
	      break;
	    }
	}

      for(int j=i;j<i+100;j++)
	for(int k=j;k<i+100;k++)
	  {
	    data.push_back(prime[j]+prime[k]);
	  }
      sort(data.begin(),data.end());
      cout<<data[p-1]<<endl;
    }
  
}