#include <bits/stdc++.h>
int tab_sieve[5000001];
unsigned long long int tab_primes[348512];
unsigned long long int *tab_primes_end = tab_primes;
unsigned long long int tab_factors[1024];
unsigned long long int *tab_factors_end = tab_factors;
unsigned long long int n, x;
unsigned long long int power(unsigned long long int base,
                             unsigned long long int power,
                             unsigned long long int modulo) {
  unsigned long long int result = base;
  for (unsigned long long int bit_mask =
           0x8000000000000000ll >> (__builtin_clzll(power) + 1ll);
       bit_mask != 0ll; bit_mask >>= 1ll) {
    result *= result;
    result %= modulo;
    if (power & bit_mask) {
      result *= base;
      result %= modulo;
    }
  }
  return result;
}
int main() {
  for (int i = 3; i <= 5000000; i += 2) {
    if (!tab_sieve[i]) {
      *tab_primes_end = (unsigned long long int)i;
      tab_primes_end++;
      for (int j = i * 3; j <= 5000000; j += i * 2) tab_sieve[j] = 1;
    }
  }
  scanf("%llu %llu", &n, &x);
  if (n == 1ll) {
    unsigned long long int b = (x & ~1ll) - 1ll;
    if (b == 1)
      printf("-1\n");
    else
      printf("%llu\n", b);
    return 0;
  } else if (n & 1ll) {
    printf("-1\n");
    return 0;
  } else {
    unsigned long long int p = n + 1ll;
    if (tab_sieve[p]) {
      printf("-1\n");
      return 0;
    }
    if ((n & 1ll) == 0ll) {
      *tab_factors_end = 2ll;
      tab_factors_end++;
      n /= 2ll;
      while ((n & 1ll) == 0) n /= 2ll;
    }
    unsigned long long int sqrt_n = (unsigned long long int)sqrt((double)n);
    for (unsigned long long int *prime_ptr = tab_primes;
         prime_ptr < tab_primes_end && *prime_ptr <= sqrt_n && n > 1;
         prime_ptr++) {
      unsigned long long int prime = *prime_ptr;
      if (n % prime == 0) {
        *tab_factors_end = prime;
        tab_factors_end++;
        n /= prime;
        while (n % prime == 0) n /= prime;
      }
    }
    if (n > 1) {
      *tab_factors_end = n;
      tab_factors_end++;
    }
    unsigned long long int end_b = x % p;
    for (unsigned long long int b = end_b - 1ll; b >= 2; b--) {
      bool success = true;
      for (unsigned long long int *factor_ptr = tab_factors;
           factor_ptr < tab_factors_end; factor_ptr++) {
        unsigned long long int factor = *factor_ptr;
        if (power(b, (p - 1ll) / factor, p) == 1ll) {
          success = false;
          break;
        }
      }
      if (success) {
        printf("%llu\n", b + x / p * p);
        return 0;
      }
    }
    if (x < p) {
      printf("-1\n");
      return 0;
    }
    if (end_b < 2) end_b = 2;
    for (unsigned long long int b = p - 1; b >= end_b; b--) {
      bool success = true;
      for (unsigned long long int *factor_ptr = tab_factors;
           factor_ptr < tab_factors_end; factor_ptr++) {
        unsigned long long int factor = *factor_ptr;
        if (power(b, (p - 1ll) / factor, p) == 1ll) {
          success = false;
          break;
        }
      }
      if (success) {
        printf("%llu\n", b + x / p * p);
        return 0;
      }
    }
    printf("-1\n");
    return 0;
  }
}
