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


using namespace std;

bool f[10000002];

int main(){
	for(int i=3;i<10000001;i+=2){f[i]=true;
		for(int j=3;j*j<=i;j+=2)
			if(i%j==0)f[i]=false;
	}
	f[2]=true;
	int n,p;
	while(1){
		cin>>n>>p;if(n==-1&&n==p)break;

		int r=0;
		int q[10001]={};
		int x=0;
		int y=n;
		while(1){
			if(f[y]==true)x++;
			if(x==p)break;
			y++;
		}
		n++;
		 vector<int> anslist;
		 for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
					anslist.push_back(i+j);
				
				cout<<i<<" "<<j<<endl;
				}
			}
			sort(anslist.begin(),anslist.end());
/*
		for(int i=n;i<=y;i++)
			for(int j=i;j<=y;j++)
			{
				if(f[i]==true&&f[j]==true){
				q[r]=i+j;r++;
				}
			}

		/*	
		sort(q,q+r);
		for(int i=0;i<r;i++)
		cout<<q[i]<<endl;

		cout<<q[p-1]<<endl;*/
			  cout << anslist[p-1] << endl;

	}
	
	return 0;
}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

const int MAX = 100000;

std::vector<int> GetPrime(int max) {
  std::vector<int> result;
  bool is_onlist[200001];
  for (int i = 2; i <= max; i++) {
    is_onlist[i] = true;
  }
  for (int i = 2; i <= max; i++) {
    if (!is_onlist[i]) continue;
    result.push_back(i);
    for (int j = i; j <= max; j += i) {
      is_onlist[j] = false;
    }
  }
  return result;
}

void Show(std::vector<int> vec) {
  for (auto x : vec) {
    cerr << "\033[93m" << x << "\033[m" << endl;
  }
}

int Solve(int n, int p, const std::vector<int>& prime) {
  // FIXME: 2+13=15も「和を並べたもの」の中に入るはずだけど，問題文の中にない
  std::multiset<int> sumset;
  int i_count = 0;
  for (int i = 0; i < prime.size(); i++) {
    if(prime[i]<=n) continue;
    int j_count = 0;
    for (int j = i; j < prime.size(); j++) {
      if (prime[j] <= n) continue;
      sumset.insert(prime[i] + prime[j]);
      j_count++;
      if (j_count > p) break;
    }
    i_count++;
    if (i_count > p) break;
  }

  auto itr = sumset.begin();
  for (int i = 1; i < p; i++) {
    // cerr << "\033[93m" << *itr << "\033[m" << endl;
    itr++;
  }
  return *itr;
}

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::vector<int> prime = GetPrime(200000);

  while (1) {
    int n, p;
    cin >> n >> p;
    if (n == -1) {
      break;
    }
    cout << Solve(n, p, prime) << endl;
  }

  return 0;
}

C++: /*
 */
#include <iostream>
#include <algorithm>

using namespace std;

// init
// fe[u
int prime[9692]; // 9,592(100,000 ÈºÌfÌ) + 100(iP ÌÅål)
int primeN;

// input
int iN; // 0 <= N <= 100,000
int iP; // 1 <= P <= 100

// manage
int iM[5050]; // 1 ~ 100 ÜÅÌa

// ú»
void init() {
  int i, j;
  bool isPrime[101198]; // 9692 ÔÚÌf = 101197

  primeN = 0;
  fill(isPrime, isPrime + 101198, true);
  isPrime[0] = isPrime[1] = false;

  for (i = 2; i < 101198; i++) {
    if (isPrime[i]) {
      prime[primeN++] = i;
      for (j = 2; i * j < 101198; j++) {
        isPrime[i * j] = false;
      }
    }
  }
}

// üÍ
bool input() {
  scanf("%d %d", &iN, &iP);
  if (iN == -1 && iP == -1) return false;
  return true;
}

// 
void manage(){
  int i, j, k;
  int cnt = 0;
  //int tmp;

  // iN æèå«¢f©ç 2 ÂðIñÅaðÆé
  for (i = 0; i < primeN; i++) {
    if (prime[i] > iN) break;
  }
  for (j = 0; j < iP; j++) {
    for (k = j; k < iP; k++) {
      iM[cnt++] = prime[i + j] + prime[i + k];
    }
  }

  // ou\[g(æª©çiPÔÜÅ\[g)
  /*
  for (j = 0; j < iP; j++) {
    for (k = cnt - 1; k > j; k--) {
      if (iM[k - 1] > iM[k]) {
        tmp = iM[k];
        iM[k] = iM[k - 1];
        iM[k - 1] = tmp;
      }
    }
  }
  */
  sort(iM, iM + cnt);
}

// oÍ
void output(){
  printf("%d\n", iM[iP - 1]);
}

// mizoSâ¤Ê
int main() {
  init(); // ú»
  while(1) {
    if (!input()) break; // üÍ + I¹»è
    manage(); // 
    output(); // oÍ
  }
  return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>

#define rep(i, j) for(int i = 0; i < j; i++)
#define rep2(i, j) for(int i = 0; i <= j; i++)
#define all(i) i.begin(), i.end()
#define pint(i) printf("%d\n", i)

using namespace std;

vector<bool> era(1000010, true);
void makePrimeTable(int n)
{
	era[0] = false; era[1] = false;
	for (int i = 2; i <= n; i++)
	{
		if (era[i]) { for (int j = i * 2; j <= n; j += i) { era[j] = 0; } }
	}
}
int main()
{
	makePrimeTable(200000);
	int N, P; while (cin >> N >> P && N != -1)
	{
		vector<int> dat(0);
		vector<int> prm(0);
		while (1)
		{
			N++;
			if (era[N]) { prm.push_back(N);}
			if (prm.size() == P) { break; }
		}
		for (int i = 0; i < P; i++) for (int j = i; j < P; j++) { dat.push_back(prm[i] + prm[j]); }
		sort(all(dat)); 
		printf("%d\n", dat[P - 1]);
	}
	return 0;
}