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<algorithm>
using namespace std;
int s[1000], k[10000], n, m, t, p, i, j;
int main() {
	while (true) {
		t = 0; p = 0;
		cin >> n >> m;
		if (n == -1) { break; }
		while (t < m) {
			if (n < 2) { goto E; }
			for (i = 2; i*i <= n; i++) {
				if (n%i == 0) {
					goto E;
				}
			}
			s[t] = n;
			t++;
		E:;
			n++;
		}
		for (i = 0; i < m; i++) { for (j = i; j < m; j++) { k[p] = s[j] + s[i]; p++; } }
		sort(k, k + p);
		cout << k[m - 1] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,p;
  vector<int>prime;
  prime.pb(2);
  for(int i=3;i<=500000;i+=2){
    bool flag=true;
    for(int j=2;j<=sqrt(i);j++){
      if(i%j==0){flag=false;break;}
    }
    if(flag)
      prime.pb(i);
  }
  while(cin>>n>>p,n+p!=-2){
    vector<int>num;
    int po;
    for(po=0;po<prime.size();po++){
      if(n<prime[po])
	break;
    }
    for(int i=po;i<po+100;i++){
      for(int j=i;j<po+100;j++){
	num.pb(prime[i]+prime[j]);
      }
    }
    sort(num.begin(),num.end());
    cout<<num[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);
  //テァツエツ?ヲツ閉ーテ」ツδェテ」ツつケテ」ツδ?
  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 <bits/stdc++.h>

using namespace std;

const int MAX = 1299709;
int prime[MAX + 1] = {0}; //0::prime 1::NOTprime;

void hurui()
{
    prime[1] = 1;
    for (int i = 1; i * i <= MAX; i++)
    {
        for (int j = 2; j <= MAX / i; j++)
        {
            if (prime[i] == 0)
                prime[i * j] = 1;
        }
    }
}

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

    std::vector<int> v;
    for (int i = 1; i <= MAX; i++)
        if (prime[i] == 0)
            v.push_back(i);
    int n, p;
    while (true)
    {
        cin >> n >> p;
        if (n == -1)
            break;
        std::vector<int> li;
        int idx = 0;
        while (v[idx] <= n)
            idx++;
        for (int i = idx; i < idx + p; i++)
        {
            for (int j = i; j < idx + p; j++)
            {
                li.push_back(v[i] + v[j]);
            }
        }
        sort(li.begin(), li.end());
        cout << li[p - 1] << endl;
    }
}