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<cmath>
using namespace std;

bool judgePrimeNumber( int a )
{
    if( a == 1 )
        return false;
    for( int i = 2; i <= sqrt(a); i++ )
        if( a % i == 0 )
            return false;
    return true;
}


int main()
{
    int n, p;
    vector<int> v, w;

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

        int i = n + 1, cnt = 0;
        while( cnt != 100 )
        {
            if( judgePrimeNumber( i ) )
            {
                v.push_back(i);
                cnt++;
            }
            i++;
        }

        for( i = 0; i < v.size() / 4; i++ )
            for( int j = i; j < v.size() / 4; j++ )
                w.push_back( v[i] + v[j] );

        for( i = 0; i < w.size(); i++ )
            for( int j = w.size() - 1; j >= i + 1; j-- )
                if( w[j] < w[j-1] )
                {
                    int t = w[j];
                    w[j] = w[j-1];
                    w[j-1] = t;
                }
        cout << w[p-1] << endl;
        v.clear();
        w.clear();
    }
    return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int L = 1e8;
const int M = 30;

bool p[L];

void make_p(){
  REP(i, L) p[i] = true;
  p[0] = p[1] = false;
  for(int i = 2; i * i <= L; ++i){
    if(!p[i]) continue;
    for(int j = i + i; j < L; j += i) p[j] = false;
  }
}

int main() {
  make_p();
  int N, P;
  while(cin >>N >>P && N != -1){
    //vector<int> _p(M), v;
    //for(int i = N + 1, cnt = 0; cnt < M; ++i) if(p[i]) _p[cnt++] = i;
    //REP(i, M) FOR(j, i, M) v.push_back(_p[i] + _p[j]);
    //SORT(v);
    //cout <<v[P - 1] <<endl;
  }
  return 0;
}

C++: /*
Hokudai Practice Contest 2014
B - Matsuzaki Number

ティツァツ」テヲツウツ陛ッツシツ堙ァツエツ?ヲツ閉ーテ・ツ按療ヲツ個凖ッツシツ凝・ツ?ィテヲツ篠「テァツエツ「
テ・ツ可催・ツ?ヲテァツ青?」ツ?ィテ」ツ?療」ツ?ヲテ」ツ??・ツ債?・ツ按?」ツ?ェテ・ツ、ツァテ」ツ?催」ツ?陛」ツ?セテ」ツ?ァテァツエツ?ヲツ閉ーテ」ツつ津ゥツ?催・ツ按療」ツ?ォテ・ツ按療ヲツ個凖」ツ?療」ツ?ヲテ」ツ?甘」ツ?催」ツ??
テ・ツ?・テ・ツ環嬾テ」ツ?ォテ・ツッツセテ」ツ?療」ツ?ヲテ・ツ、ツァテ」ツ?催」ツ?貧テ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??ァツエツ?ヲツ閉ーテ」ツつ津ゥツ?催・ツ按療」ツ?凝」ツつ嘉ヲツ篠「テ」ツ?凖」ツ??
ティツヲツ凝」ツ?、テ」ツ?妥」ツ?淌ァツエツ?ヲツ閉ーテ」ツ?凝」ツつ英テ・ツ?凝」ツつ津・ツ渉姪」ツつ甘・ツ?コテ」ツ?療」ツ??」ツ?凖」ツ?ケテ」ツ?ヲテ」ツ?ョテ・ツ渉ッティツδステ」ツ?ェテ」ツδ堙」ツつ「テ」ツ?ョテ・ツ陳古」ツつ津・ツ按療ヲツ個凖」ツ?凖」ツつ凝」ツ??
テ」ツ?敕」ツ?ョテ、ツクツュテ」ツ?ァpテァツ閉ェテァツ崢ョテ」ツ?ォテ・ツーツ湘」ツ?陛」ツ??」ツつづ」ツ?ョテ」ツ?古ァツュツ氾」ツ?暗」ツ?ィテ」ツ?ェテ」ツつ凝」ツ??

テ・ツ按カテァツエツ?」ツ??0^6テ」ツ?ィテ・ツ、ツァテ」ツ?催」ツつ?」ツ?ェテ」ツ?ョテ」ツ?ァテ」ツ??」ツつィテ」ツδゥテ」ツδ暗」ツつケテ」ツδ?」ツδ催」ツつケテ」ツ?ョテァツッツゥテ」ツつ津ァツ板ィテ」ツ??」ツ?ヲテァツエツ?ヲツ閉ーテ・ツ按療ヲツ個凖」ツつ津ィツ。ツ古」ツ??

テヲツウツィテヲツ?湘ァツつケテ」ツ?ィテ」ツ?療」ツ?ヲテ」ツ?｝^(1/2)テ」ツ?セテ」ツ?ァテ・ツ渉姪」ツつ甘・ツ?コテ」ツ?療」ツ?ヲテ」ツδ堙」ツつ「テ」ツ?ョテ・ツ陳古」ツつ津ィツィツ暗ァツョツ療」ツ?凖」ツつ凝」ツ??」ツ?妥」ツ?ァテ」ツ?ッテ」ツ??
テ・ツ陳古」ツ?継テァツ閉ェテァツ崢ョテ」ツ?ォテ・ツーツ湘」ツ?陛」ツ??ヲツ閉ーテ」ツ?古ァツ渉セテ」ツつ古」ツつ凝」ツ?ィテ」ツ?ッテゥツ卍静」ツつ嘉」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテ、ツクツ催・ツ債?・ツ按?」ツ?ァテ」ツ?づ」ツつ?

[テ・ツ可催・ツ?ヲテァツ青?
テヲツ卍づゥツ鳴禿ィツィツ暗ァツョツ療ゥツ?湘ッツシツ唹(MAX_N loglog MAX_N)
テァツゥツコテゥツ鳴禿ィツィツ暗ァツョツ療ゥツ?湘ッツシツ唹(MAX_N)
[1テ」ツδ?」ツつケテ」ツδ暗」ツつアテ」ツδシテ」ツつケテ」ツ?ョテ・ツ?ヲテァツ青?
テヲツ卍づゥツ鳴禿ィツィツ暗ァツョツ療ゥツ?湘ッツシツ唹(logMAX_N + p^2 log p)
テァツゥツコテゥツ鳴禿ィツィツ暗ァツョツ療ゥツ?湘ッツシツ唹(p^2)
*/

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

//10^6テ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??ァツエツ?ヲツ閉ーテ」ツつ?00テ・ツ?凝ィツヲツ凝」ツ?、テ」ツ?妥」ツつ凝・ツソツ?ィツヲツ?」ツ?古」ツ?づ」ツつ凝」ツ?ョテ」ツ?ァテ」ツ??0^6テ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツつ?」ツ?ォテゥツ?催・ツ按療」ツつ津ァツ「ツコテ、ツソツ敕」ツ?凖」ツつ?
//10^6テ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??00テァツ閉ェテァツ崢ョテ」ツ?ョテァツエツ?ヲツ閉ーテ」ツ?ッ101197テ」ツ?ェテ」ツ?ョテ」ツ?ァテ」ツ??・ツョツ淌ゥツ堋崚」ツ?ッMAX_N=101198テ」ツ?ァテ・ツ債?・ツ按?
const int MAX_N = 200000;

int main(){
  //テァツエツ?ヲツ閉ーティツ。ツィ
  vector<bool> isPrime(MAX_N+1,1);
  //テァツエツ?ヲツ閉ーテ」ツδェテ」ツつケテ」ツδ?
  vector<int> primes;
  isPrime[0] = isPrime[1] = 0;

  //テ」ツつィテ」ツδゥテ」ツδ暗」ツつケテ」ツδ?」ツδ催」ツつケテ」ツ?ョテァツッツゥ
  for(int i=2;i<=MAX_N;i++){
    if(!isPrime[i])continue;
    primes.push_back(i);
    for(int j=2*i;j<=MAX_N;j+=i)isPrime[j] = 0;
  }
 
  int n,p;
  while(cin >> n >> p, n>=0){ 
    //nテ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??ヲツ慊?・ツーツ湘」ツ?ョテァツエツ?ヲツ閉ーテ」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ヲツアツづ」ツつ?」ツつ?テ」ツ?禿」ツ?禿」ツ?ァテ」ツ?ッテ、ツコツ古・ツ按?ヲツ篠「テァツエツ「)
    int pos = upper_bound(primes.begin(),primes.end(),n) - primes.begin();

    //nテ」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ??ァツエツ?ヲツ閉ーテ」ツつ津・ツーツ湘」ツ?陛」ツ??ヲツ鳴ケテ」ツ?凝」ツつ英テ・ツ?凝」ツδェテ」ツつケテ」ツδ暗」ツ?ォティツゥツーテ」ツつ?」ツつ?
    vector<int> pList;
    for(int i=0;i<p;i++)pList.push_back(primes[pos+i]);

    //テ」ツ?凖」ツ?ケテ」ツ?ヲテ」ツ?ョテ」ツδ堙」ツつ「テ」ツ?ョテ・ツ陳古」ツつ津ヲツアツづ」ツつ?」ツつ?
    vector<int> sum;
    for(int i=0;i<p;i++){
      for(int j=0;j<=i;j++){
	sum.push_back(pList[i] + pList[j]);
      }
    }
 
    //pテァツ閉ェテァツ崢ョテ」ツ?ォテ・ツーツ湘」ツ?陛」ツ??ヲツ閉ーテ」ツ?ッテ」ツ??」ツつステ」ツδシテ」ツδ暗」ツ?療」ツ?ヲsum[p-1]テ」ツ?ォテ」ツつ「テ」ツつッテ」ツつサテ」ツつケテ」ツ?凖」ツつ古」ツ?ーテ」ツつ湘」ツ?凝」ツつ?
    sort(sum.begin(),sum.end());
    cout << sum[p-1] << endl;
  }
}