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 <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

using Tup = tuple<int64, int64, int64>;

vector<int64> prime;

void init(){
	bool notPrime[112345] = {};
	notPrime[0] = notPrime[1] = 1;
	FOR(i, 2, 112345){
		if(!notPrime[i]){
			prime.push_back(i);
			for(int64 j = 2;i*j < 112345;j++){
				notPrime[i*j] = 1;
			}
		}
	}
}

int main(void){
	init();
	int64 N, P;
	while(cin >> N >> P && N+P != -2){
		int32 idx = upper_bound(all(prime), N)-prime.begin();
		vector<int64> v1(100);
		REP(i, 100){
			v1[i] = prime[idx+i];
		}
		vector<int64> v2;
		REP(i, 100){
			FOR(j, i, 100){
				v2.push_back(v1[i]+v1[j]);
			}
		}
		sort(all(v2));
		cout << v2[P-1] << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
#include<list>
#include<set>
using namespace std;
#define fr first
#define sc second
#define INFTY 1000000
typedef pair< int,int > iiP;
typedef pair< int,iiP > P;
int main(){
  int N,P;
  bool Prime[INFTY + 1]={0};
  vector<int> Matsuzaki;
  vector<int> Prime_No;
  for(int i=2;i<INFTY;i++){
    if(!Prime[i]){
      Prime_No.push_back(i);
      for(int j=2;i*j<INFTY;j++) Prime[i*j] = true;
    }
  } 
  while(cin >> N >> P) {
    if(N == -1 && P == -1) break;
    int s =  upper_bound(Prime_No.begin(),Prime_No.end(),N) - Prime_No.begin();
    for(int i=0;i<=100;i++){
      for(int j=i;j<=i + 100;j++){
	Matsuzaki.push_back(Prime_No[s + i] + Prime_No[s + j]);
      }
    }
    sort(Matsuzaki.begin(),Matsuzaki.end());
    cout << Matsuzaki[P - 1] << endl;
    Matsuzaki.clear();
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define loop for(;;)
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;
#define inf (1e9)
#define eps (1e-9)

template<class S, class T> inline
ostream& operator<<(ostream&os, pair<S,T> p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template<class S, class T, class U> inline
ostream& operator<<(ostream&os, tuple<S,T,U> t) {
  return os << '('
    << get<0>(t) << ", "
    << get<1>(t) << ", "
    << get<2>(t) << ')';
}

template<class T> inline
ostream& operator<<(ostream&os, vector<T> v) {
  if (v.size() == 0) { return os << "(empty)"; }
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

template<class T> inline
istream& operator>>(istream&is, vector<T>&v) {
  rep (i, v.size()) is >> v[i];
  return is;
}

bool* PrimeSieve(int n) {
  bool*s = new bool[n];
  for (int i = 0; i < n; ++i) s[i] = true;
  s[0] = s[1] = false;
  for (int i = 2; i < n; ++i)
    if (s[i]) for (int j = i << 1; j < n; j += i) s[j] = false;
  return s;
}

typedef vector<int> vi;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(10);

  bool* ps = PrimeSieve(100000 * 2);
  loop {
    int n, p; cin >> n >> p;
    if (n < 0 and p < 0) break;

    vi xs;
    for (int i = n + 1, j = 0; j < p; ++i) {
      if (ps[i]) {
        ++j;
        xs.push_back(i);
      }
    }

    vi ys;
    for (int x: xs) {
      for (int y: xs) {
        if (y > x) break;
        ys.push_back(x + y);
      }}
    sort(begin(ys), end(ys));
    cout << ys[p-1] << endl;
  }

  return 0;
}