#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int INF=0x3f3f3f3f;
const int MOD=998244353;
const int MAX_SIZE=6e5+10;
//map NO. to id
const int N=1e4+5;
void solve(){
ll a,b;
cin>>a>>b;
if(__gcd(a,b)!=1){
cout<<0<<endl;
return;
}
if(a<b)swap(a,b);
//gcd(a+c,b+c)=gcd(a+c,a-b)
//count the factor of a-b;
ll c=a-b;
if(c==1||a+b==2){
cout<<-1<<endl;
return;
}
vector<ll>fac;
for(ll i=2;i<=pow(c,0.5);i++){
if(c%i==0){
fac.push_back(i);
fac.push_back(c/i);
}
}
fac.push_back(c);
ll ans=1e18;
for(auto &v:fac){
ll tmp=a%v;
if(tmp==0) {
ans=0;
break;
}
ans=min(v-tmp,ans);
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--)solve();
}