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>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return (x<0||x>=n||y<0||y>=m)?false:true;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
typedef pair<int,int> P;

vector<int> sieve(int n) {
  vector<int> p(n);
  REP(i,2,n) p[i]=i;
  for(int i=2; i*i<n; i++) {
    if(p[i]) {
      for(int j=i*i; j<n; j+=i) p[j]=0;
    }
  }
  //p.erase(remove(all(p),0),p.end());
  return p;
}

int main() {
  vector<int> p=sieve(1000000);
  int m[1000001];
  m[0]=0;
  REP(i,1,p.size()) {
    m[i]=m[i-1];
    if(p[i]) m[i]++;
  }
  p.erase(remove(all(p),0),p.end());
  int n,k;
  while(cin >> n >> k) {
    if(n==-1 && k==-1) break;
    vector<int> a;
    int x=m[n];
    REP(i,x,x+101) {
      REP(j,i,x+101) a.pb(p[i]+p[j]);
    }
    sort(all(a));
    pr(a[k-1]);
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

vector<int> primes;

bool isPrime(int p) {
  for (int i = 0; i < primes.size() && primes[i] <= sqrt(p); i++) {
    if (p % primes[i] == 0) {
      return false;
    }
  }
  return true;
}

void makePrimes(int n) {
  primes.pb(2);
  for (int i = 3; i < n; i += 2) {
    if (isPrime(i)) {
      primes.pb(i);
    }
  }
}

int main() {
  makePrimes(300000);
  int n, p;
  while (cin >> n >> p) {
    if (n == -1 && p == -1) break;
    vector<int> sum_list;
    int index;
    for (index = 0; index < primes.size(); index++) {
      if (n < primes[index]) break;
    }
    for (int i = index; i < index+100; i++) {
      for (int j = i; j < index+100; j++) {
        sum_list.pb(primes[i]+primes[j]);
      }
    }
    sort(sum_list.begin(), sum_list.end());
    cout << sum_list[p-1] << el;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

// eratosthenes の篩（素数かどうかの表が返る）
vector<bool> eratosthenes( int n )
{
	vector<bool> nums( n + 1, true );
	nums[0] = nums[1] = false;

	for ( int i = 0; i < nums.size(); ++i )
	{
		if ( !nums[i] )
		{
			continue;
		}

		for ( int j = 2; i * j < nums.size(); ++j )
		{
			nums[ i * j ] = false;
		}
	}

	return nums;
}

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

	VI primes;
	{
		vector<bool> isPrime( eratosthenes( 1000000 ) );
		REP( i, 0, isPrime.size() )
		{
			if ( isPrime[i] )
			{
				primes.PB( i );
			}
		}
	}

	while ( true )
	{
		int n, p;
		cin >> n >> p;

		if ( ( n & p ) == -1 )
		{
			break;
		}

		int minpos = upper_bound( ALL( primes ), n ) - primes.begin();

		VI nums;
		REP( i, 0, p )
		{
			REP( j, i, p )
			{
				nums.PB( primes[ minpos + i ] + primes[ minpos + j ] );
			}
		}
		sort( ALL( nums ) );

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

	return 0;
}