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++: /*
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;
  }
}

C++: #include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)
#define N 100510

bool era[N] = {true, true};

signed main(void){
  int i, j, k;

  for(i = 2;i * i <= N;i++){
    if(!era[i])for(j = i * i;j < N;j+=i)era[j] = true;
  }

  for(int testcase = 0;testcase < 1234567;testcase++){

    for(;;){
      int2(n, p);
      if((n & p) == -1)break;
      int pri[p], h = 0;
      for(i = n + 1;h < p;++i)if(!era[i])pri[h++] = i;

      Vint lis;
      rep(i, p)rep(j, i + 1)lis.pb(pri[i] + pri[j]);
      sort(all(lis));

      pri(lis[p - 1]);
    }

/*/

//*/ break;
  }
  return 0;
}