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 <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int const N = 200000;
  vector<int> prime(N);
  REP(i,N)
    prime[i] = i;
  prime[0]=prime[1]=0;
  REP(i,N)
    if (prime[i])
      for (int j=i*2; j<N; j+=i) 
        prime[j] = 0;
  prime.erase(remove(ALL(prime), 0), prime.end());
  int n,p;
  while(cin>>n>>p,n>=0) {
    vector<int> v;
    int cnt = 0;
    vector<int>::iterator hoge = upper_bound(ALL(prime), n);
    for (vector<int>::iterator it = hoge; cnt<p; ++cnt, ++it) {
      int cnt2 = 0;
      for (vector<int>::iterator jt = it; cnt2<=p-cnt; ++cnt2, ++jt) {
//        cout << *it << " " << *jt << endl;
        v.push_back(*it + *jt);
      }
    }
    sort(ALL(v));
    // FOR(it, v)
    //   cout << *it << " ";
    // cout << endl;
    cout << v[p-1] << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(void){
  vector<int>num,ans;
  int n,p,prime[1000001];

  for(int i=0;i<1000001;i++)prime[i]=0;
  prime[0]=prime[1]=1;

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


  while(cin >> n >> p){
    if(n<0 && p<0)break;
    num.clear();
    ans.clear();

    for(int i=n+1;i<n+p+1000;i++)
      if(prime[i]==0)num.push_back(i);

    for(int i=0;i<num.size();i++){
      for(int j=i;j<num.size();j++){
	ans.push_back(num[i]+num[j]);
      }
    }
    sort(ans.begin(),ans.end());

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

  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int m = 200000, N, P, t, c;
  vector<bool> Prime(m, true);
  Prime[0] = false;
  Prime[1] = false;
  vector<int> V, M;
  for (int i = 2; i*i < m; ++i) for (int j = i; i*j < m; ++j) Prime[i*j] = false;
  while (cin >> N >> P, N != -1) {
    t = N+1;
    c = P;
    while (c > 0) {
      if (Prime[t]) {
	V.push_back(t);
	--c;
      }
      ++t;
    }
    for (int i = 0; i < P; ++i) {
      for (int j = i; j < P; ++j) {
	M.push_back(V[i]+V[j]);
      }
    }
    sort(M.begin(), M.end());
    cout << M[P-1] << endl;
    V.erase(V.begin(), V.end());
    M.erase(M.begin(), M.end());
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
  int n,p;
  int prime[200000];

  for(int i=0;i<200000;i+=2)prime[i] = 0;
  for(int i=1;i<200000;i+=2)prime[i] = 1;
  prime[1] = 0;
  prime[2] = 1;

  int c = 3;
  while(c*c<200000){
    for(int i=3*c;i<200000;i+=c)prime[i] = 0;
    while(!prime[++c]);
  }

  while(1){
    cin >> n >> p;
    if(n<0 && p<0)break;

    vector<int> e;
    for(int i=0;i<p;i++){
      while(!prime[++n]);
      e.push_back(n);
    }

    vector<int> c;
    for(int i=0;i<(int)e.size();i++){
      for(int j=i;j<(int)e.size();j++){
	c.push_back(e[i] + e[j]);
      }
    }

    sort(c.begin(),c.end());
    cout << c[p-1] << endl;
  }
}

C++: #include <iostream>
#define N 150000
using namespace std;
int main(){
  int n,p,sosu[N],i,j,result,data[N],l,flag,temp;
  for(i=0;i<N;i++) sosu[i]=1;
  sosu[0]=0;
  sosu[1]=0;
  for(i=2;i<N/2;i++){
    if(sosu[i]==1){
      for(j=i*2;j<N;j+=i){
        sosu[j]=0;
      }
    }
  }
  while(1){
    flag=0;
    l=0;
    cin >> n >> p;
    if(n==-1&&p==-1) break;
    for(i=n+1;i<N;i++){
      if(sosu[i]==1){
	for(j=n+1;j<=i;j++){
          if(sosu[j]==1){
            data[l]=i+j;
	    l++;
	    if(l==p+300){
	      flag=1;
	      break;
	    }
	  }
	}
	if(flag==1) break;
      }
    }
    for(i=1;i<p+300;i++){
      for(j=1;j<p+300;j++){
	if(data[j]<=data[j-1]){
	  temp=data[j];
	  data[j]=data[j-1];
	  data[j-1]=temp;
	}
      }
    }
    cout << data[p-1] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)

using namespace std;

int N, P;

int main() {
  vector<int> v;
  bool flag[150000];
  REP(i,0,150000) flag[i] = true;
  for(int i = 2; i<150000; i++){
    if(!flag[i]) continue;
    v.push_back(i);
    for(int j = i*2; j<150000; j += i){
      flag[j] = false;
    }
  }

  while(1){
    cin>>N>>P;
    if(N==-1&&P==-1) break;
    int pos;
    REP(i,0,v.size()){
      if(N < v[i]){
        pos = i;
        break;
      }
    }
    vector<int> nums;
    REP(i,pos,v.size()){
      REP(j,i,v.size()){
        int num = v[i]+v[j];
        nums.push_back(num);
      }
    }
    sort(nums.begin(),nums.end());
    cout<<nums[P-1]<<endl;
  }

  return 0;
}