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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
bool b[200000] = { false };
int main() {
	vi pri;
	FOR(i,2, 200000) {
		if (!b[i]) {
			pri.push_back(i);
			for (int j = i * 2;j < 200000;j += i) {
				b[j] = true;
			}
		}
	}
	int n, p;
	while (cin >> n >> p, n != -1) {
		int l = 0;
		int r = pri.size();
		while (abs(l-r)>1) {
			if (pri[(l + r) / 2]>n) {
				r = (l + r) / 2;
			}else{
				l = (l + r) / 2;
			}
		}
		if (pri[l] > n)r = l;
		vi matsuzaki;
		REP(i, 100) {
			FOR(j,i, 100+i) {
				matsuzaki.push_back(pri[r+i]+ pri[r + j]);
			}
		}
		SORT(matsuzaki);
		cout << matsuzaki[p - 1] << endl;
	}
}

C++: #include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;

class Solver {
public:
  Solver();
  void solve(int N, int P);
private:
  bool isprime[200000];
};

Solver::Solver() {
  for(int i = 0; i < 200000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for(int i = 2; i < 2000; ++i) {
    if( isprime[i] ) {
      for(int j = i * 2; j < 200000; j+=i) {
        isprime[j] = false;
      }
    }
  }
}

void Solver::solve(int N, int P) {
  std::vector<int> can;
  for(int i = N + 1; i < std::min(N + P * 4 + 1000, 200000); ++i) {
    if( not isprime[i] ) continue;
    for(int j = i; j < std::min(N + P * 4 + 1000, 200000); ++j) {
      if( not isprime[j] ) continue;
      can.push_back(i + j);
    }
  }
  std::sort(std::begin(can), std::end(can));
  std::cout << can[P-1] << std::endl;
}


int main() {

  Solver solver;
  
  for(;;) {
    int N, P;
    std::cin >> N >> P;
    if( N == 0 and P == 0 ) break;
    
    solver.solve(N, P);
  }  
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define UNIQ(v) do { sort(ALL(v)); v.erase(unique(v.begin(), v.end()), v.end()); } while(0)

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", "<< z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 102000
bool prime[MAX];

void erat()
{
	fill(prime, prime+MAX, true);
	prime[0] = false;
	prime[1] = false;
	for (int i = 2; i < MAX; i++)
		for (int j = 2*i; j < MAX; j += i)
			prime[j] = false;
}

int main(void)
{
	int n, p;
	erat();
	while (cin >> n >> p, !(n == -1 && p == -1)) {
		vector<int> vs;
		vector<int> ans;
		for (int i = n+1, cnt = 0; cnt <= p; i++)
			if (prime[i])
				vs.push_back(i), cnt++;

		rep(i, vs.size())
			REP(j, i, vs.size())
				ans.push_back(vs[i]+vs[j]);
		sort(ALL(ans));

		cout << ans[p-1] << endl;
	}
	return 0;
}