#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 s = a + b; ll ans = -1; for(ll i = 1; i <= s; i++){ if(s % i != 0) continue; if((i+a)%b == 0 && (i+b)%a == 0){ ans = i; break; } } cout << ans << endl; }