#include <bits/stdc++.h>
using namespace std;
int n;
inline int Pow(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = a * 1ll * a % n)
    if (b & 1) c = c * 1ll * a % n;
  return c;
}
inline bool cao(int x) {
  int p = n - 1;
  for (int i = 1; i * i <= n; i++)
    if (p % i == 0) {
      if (i < p && Pow(x, i) == 1) return 0;
      if (p / i < p && Pow(x, p / i) == 1) return 0;
    }
  return 1;
}
int main() {
  int x;
  scanf("%d%d", &n, &x);
  n++;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) {
      printf("-1\n");
      return 0;
    }
  for (x--; x > 1; x--)
    if (cao(x)) {
      printf("%d\n", x);
      return 0;
    }
  printf("-1\n");
  return 0;
}
