#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define N (1<<17)
#define M 1000000007
using namespace std;
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
int main () {
int n, m;
cin>>n>>m;
if (n == m) cout<<0<<endl;
else if (n < m) {
int ans = n * (m % n);
m -= m % n;
if (n == m) cout<<ans<<endl;
else {
m /= n;
ans += n * m - n;
cout<<ans<<endl;
}
} else {
n = n % m;
if (!n) cout<<0<<endl, exit(0)<<endl;
int ans = n * (m % n);
m -= m % n;
if (n == m) cout<<ans<<endl;
else {
m /= n;
ans += n * m - n;
cout<<ans<<endl;
}
}
}