#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10100;
int N, X;
int flist[MAXN];
int nfac;
long long gcf(long long left, long long right) {
  if (right == 0) return left;
  return gcf(right, left % right);
}
long long mpow(long long b, long long p, long long m) {
  long long res = 1;
  long long p2 = b;
  for (int i = 0; i < 23; i++) {
    if (p & (1 << i)) res = (res * p2) % m;
    p2 = (p2 * p2) % m;
  }
  return res;
}
bool works(int base) {
  for (int i = 0; i < nfac; i++)
    if (mpow(base, flist[i], N + 1) == 1) return false;
  return mpow(base, N, N + 1) == 1;
}
bool prime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) return false;
  return true;
}
int main() {
  cin >> N >> X;
  if (N == 1) {
    if (X == 2) {
      cout << "-1\n";
      return 0;
    }
    cout << X - 1 << "\n";
    return 0;
  }
  nfac = 0;
  for (int i = 1; i < N; i++) {
    if (N % i == 0) {
      flist[nfac] = i;
      nfac++;
    }
  }
  if (!prime(N + 1)) {
    cout << "-1\n";
    return 0;
  }
  for (int i = X - 1; i >= 2; i--) {
    if (works(i)) {
      cout << i << "\n";
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
