#include <iostream>

using namespace std;

bool prime(unsigned long long int  a,unsigned long long int  b)
{
  int t;
  while(b)
  {
      t=b;
      b=a%b;
      a=t;
  }
  return a==1;
}

int main()
{
    unsigned long long int a,b,step=0;
    cin>>a>>b;

        while(!prime(a,b) && a<=b){
            if((step++)%2==0)a++;
            else b--;
        }
       if(a<=b)
        cout<<b-a+1;
        else
    cout<<-1;
    return 0;
}