#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define N 200000
#define INT(X) \
  ( *( (const int *)(X) ) )

int pc = 0;
int pv[ N + 1 ] = { 1, 1, 0 };

int compare (
  const void  * a,
  const void  * b
  )
{
  return ( INT( a ) - INT( b ) );
}

/** Application main entry point. */
int main (
  int     argc,
  char  * argv[ ]
  )
{
  int i, j;

  for ( j = 2; j <= N; ++j )
  {
    if ( pv[ j ] ) continue ;

    for ( i = j * 2; i <= N; i += j )
    {
      pv[ i ] = 1;
    }
  }
  for ( i = 2; i <= N; ++i )
  {
    if ( !pv[ i ] )
    {
      pv[ pc++ ] = i;
    }
  }

  for ( ; ; )
  {
    int d[ 5050 ], dc = 0;
    int n, p, b;

    scanf ( "%d%d", &n, &p );
    if ( !~( n & p ) ) break ;

    for ( b = 0; pv[ b ] <= n; ++b ) ;

    for ( j = 0; j < 100; ++j )
    for ( i = j; i < 100; ++i )
    {
      d[ dc++ ] = pv[ b + j ] + pv[ b + i ];
    }
    qsort ( d, dc, sizeof ( int ), compare );

    printf ( "%d\n", d[ p - 1 ] );
  }

  return ( 0 );
}