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 <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <cmath>
#include <algorithm>
#include <map>
#include <list>
#include <cstdlib>

using namespace std;

typedef pair<int, int> P;

#define MAX 1000000

int main()
{
	bool is_prime[MAX] = {false};
	for(int i = 3; i < MAX; i += 2) {
		is_prime[i] = true;
	}
	is_prime[2] = true;
	for(int i = 3; i * i < MAX; i += 2) {
		if(!is_prime[i]) continue;
		for(int j = i * 2; j < MAX; j += i) {
			is_prime[j] = false;
		}
	}

	while(1) {
		int N, P;
		multiset<int> set_sum_p;
		int first_p;
		int ans;

		scanf("%d %d", &N, &P);
		if(N == -1 && P == -1) break;

		for(int i = N + 1; i < MAX; i++) {
			if(!is_prime[i]) continue;
			first_p = i;
			break;
		}
		int a = first_p, b = first_p;
		while(a < MAX) {
			if(!is_prime[a]) {
				a++;
				continue;
			}
			if(set_sum_p.size() >= P) {
				multiset<int>::iterator it = set_sum_p.begin();
				for(int i = 1; i < P; i++) {
					it++;
				}
				if(a + first_p > *it) {
					ans = *it;
					break;
				}
			}
			b = first_p;
			while(b <= a) {
				if(!is_prime[b]) {
					b++;
					continue;
				}
				set_sum_p.insert(a + b);
				b++;
			}
			a++;
		}
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define ENDL endl
using namespace std;
int n;
string A, B;
int f(char c) {
  if (c == 'm')
    return 1000;
  if (c == 'c')
    return 100;
  if (c == 'x')
    return 10;
  return 1;
}
int main() {
  cin >> n;
  char r[4] = {'m', 'c', 'x', 'i'};
  while (cin >> A >> B) {
    int a = 0, b = 0, c;
    rep(i, A.size()) {
      rep(j, 4) if (A[i] == r[i]) {
        if (i != 0) {
          if (A[i - 1] != 'm' && A[i - 1] != 'i' && A[i - 1] != 'c' &&
              A[i - 1] != 'x') {
            c = A[i - 1] - '0';
            a += c * f(A[i]);
          } else
            a += f(A[i]);
        } else if (i == 0)
          a += f(A[i]);
      }
    }
    rep(i, B.size()) {
      rep(j, 4) if (B[i] == r[i]) {
        if (i != 0) {
          if (B[i - 1] != 'm' && B[i - 1] != 'i' && B[i - 1] != 'c' &&
              B[i - 1] != 'x') {
            c = B[i - 1] - '0';
            b += c * f(B[i]);
          } else
            b += f(B[i]);
        } else
          b += f(B[i]);
      }
    }
    cout << a + b << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<iomanip>
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ?2e18:1e9+10;
int main(){
  int n,m;
  vi sos(200000);
  sos[0]=sos[1]=1;
  for(int i=2;i*i<=200000;i++)if(sos[i]==0)
				for(int j=i*2;j<200000;j+=i)sos[j]=1;
  while(cin>>n>>m,n+1){
    vi a,b;
    loop(i,n+1,200000)if(sos[i]==0){
      a.pb(i);
      if(a.size()>m)break;
    }
    rep(i,a.size())rep(j,a.size())if(i<=j)b.pb(a[i]+a[j]);
    sort(all(b));
    cout<<b[m-1]<<endl;
  }
}

C++: #include <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair< int, int > iP;
typedef pair< iP, int > iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 1000000

vector< int > p;
vector< bool > isP(MAX, true);

void makePrime() {
    isP[0] = isP[1] = false;
    for(int i = 2; i * i < MAX; i++)
        for(int j = i + i; j < MAX; j += i)
            isP[j] = false;

    for(int i = 2; i < MAX; i++) {
        if(isP[i]) p.push_back(i);
    }
}

int main() {

    makePrime();

    int N, P;
    while(cin >> N >> P, ~N) {
        int off = upper_bound(p.begin(), p.end(), N) - p.begin();

        vector< int > M;
        for(int i = 0; i < P; i++) {
            for(int j = i; j < P; j++) {
                M.push_back(p[off + i] + p[off + j]);
            }
        }
        sort(M.begin(), M.end());
        cout << M[P - 1] << endl;
    }
}