#include #include #include #include #include #include #define Max(a, b) ((a) > (b) ? (a) : (b)) #define Min(a, b) ((a) > (b) ? (b) : (a)) #define abs(x) ((x) > 0 ? (x) : -(x)) #define MOD (ll)1000000007 //10^9 + 7 #define endl printf("\n") typedef long long ll; int asc(const void *a, const void *b) { ll x = *(ll*)a, y = *(ll*)b; if (x > y) return 1; if (x < y) return -1; return 0; } int main(int argc, char *argv[]) { ll a, b; scanf("%lld %lld", &a, &b); ll n = a + b, d[100000], k = 0; for (ll i = 1; i * i <= n; i++) { if (!(n % i)) { d[k++] = i; d[k++] = n / i; } } qsort(d, k, sizeof(ll), asc); int l = 0; ll x, c; while(l < k) { c = d[l]; x = !((a + c) % b) + !((b + c) % a); if (x == 2) { printf("%lld\n", c); return 0; } l++; } printf("-1\n"); return 0; }