Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.
Neko has two integers aa and bb. His goal is to find a non-negative integer kk such that the least common multiple of a+ka+k and b+kb+k is the smallest possible. If there are multiple optimal integers kk, he needs to choose the smallest one.
Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?
Input
The only line contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).
Output
Print the smallest non-negative integer kk (k≥0k≥0) such that the lowest common multiple of a+ka+k and b+kb+k is the smallest possible.
If there are many possible integers kk giving the same value of the least common multiple, print the smallest one.
Examples
Input
6 10
Output
2
Input
21 31
Output
9
Input
5 10
Output
0
Note
In the first test, one should choose k=2k=2, as the least common multiple of 6+26+2and 10+210+2 is 2424, which is the smallest least common multiple possible.
译:z=gcd(a+k,b+k);
所以:(a+k)%z==0,(b+k)%z==0;
(a+k)%z==(b+k)%z;
a%z+k%z=b%z+k%z;
故 (a-b)%z=0;
然而 lcm=(a+k)*(b+k)/z;
使得z最大即可; z又是(a-b)的约数;(a-b)为定值;
根据:(a+k)%z==0,所以k=z-a%z; ps: 因为使a可以整除z往上找,k必定小于z;
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mmax,sum=0,a,b;
ll gcd(ll x,ll y)
{
return x%y==0?y:gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
return x*y/gcd(x,y);
}
void solve(int x)
{
int tem;
tem=x-a%x;
if(lcm(a+tem,b+tem)<mmax)
{
sum=tem;
mmax=lcm(a+tem,b+tem);
}
}
int main()
{
int c;
cin>>a>>b;
if(a<b)swap(a,b);
mmax=lcm(a,b);
c=a-b;
for(int i=1;i*i<=c;i++)
{
if(c%i==0)
{
solve(i);
solve(c/i);
}
}
cout<<sum<<endl;
return 0;
}