/*
TASK: crazy
LANG: C++
*/
#include <iostream.h>
#include <math.h>
#include "module.h"
long x,y;
long masx[1000];
long masy[1000];
long br;
long pr[] = {2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499};
long isprime(long n)
{ long i = 0;
  if (n==2) return 1; 
  while (pr[i]<=n)
  { if (pr[i] == n) return 1;
    i++;
  }
  return 0;

  //if (n==2) return 1;
  //for(long i=2; i<sqrt(n); i++) if (n%i==0) return 0;
  //return 1;
}
void fill(long x, long y)
{ br=0;
  long i;
  long buf;
  for(i=0; pr[i]<x; i++)
  if (x%pr[i]==0)
  { masx[br] = pr[i];
    masy[br] = y+1;
    br++;
    buf = pr[i]+pr[i];
    while(buf<x)
    { masx[br] = buf;
      masy[br] = y+1;
      br ++;
      buf=buf+pr[i];
    }
  }
  for(i=0; pr[i]<y; i++)
  if (y%pr[i]==0)
  { masx[br] = x+1;
    masy[br] = pr[i];
    br++;
    buf = pr[i]+pr[i];
    while(buf<y)
    { masx[br] = x+1;
      masy[br] = buf;
      br ++;
      buf=buf+pr[i];
    }
  }

}
void show(void)
{ long i;
  for (i=0; i < br; i++) cout << masx[i] << " " << masy[i] << endl;
}
void back(void)
{ long i;
  for(i=0; i<br; i++)
  if (isprime(masx[i]) && isprime(masy[i]))
  { setnum(masx[i],masy[i]);
    return;
  }
  for(i=0; i<br; i++) if ((isprime(masx[i])&&(!isprime(masx[i]+1))) || (isprime(masy[i])&&(!isprime(masy[i]+1))))
  { setnum(masx[i],masy[i]);
    return;
  }
  for(i=0; i<br; i++) if (masx[i]==masy[i])
  { setnum(masx[i],masy[i]);
    return;
  }
  for(i=0; i<br; i++) if (isprime(masx[i]) || isprime(masy[i]))
  { setnum(masx[i],masy[i]);
    return;
  }
  setnum(masx[br-1], masy[br-1]);
}
void solve(void)
{  while(1)
   { getnum(&x,&y);
     fill(x,y);
     back();
   }

}
int main(void)
{ solve();
  return 0;
}