#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using P = pair; using vi = vector; using vl = vector; ld EPS = 1e-12; int INF = numeric_limits::max() / 2; ll LINF = numeric_limits::max() / 2; int MOD = 1e9 + 7; #define rep(i,n) for(int i = 0; i < n; i++) #define all(obj) (obj).begin(), (obj).end() #define debug(x) cerr << #x << ": " << x << '\n' int main() { cin.tie(0); ios::sync_with_stdio(false); ll a,b; cin >> a >> b; ll ans = -1; ll ab = a + b; for(int i = 1; i * i <= ab; i++){ if(ab % i != 0) continue; ll next = ab / i; if((i+a)%b == 0 && (i+b)%a == 0){ ans = i; break; } if((next+a)%b == 0 && (next+b)%a == 0){ ans = next; break; } } cout << ans << endl; }