#include <bits/stdc++.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll A,B;

const ll INF=1e18;

int main(){
  cin>>A>>B;
  ll ans=INF;
  for(ll i=1;i<A;i++){
    ll x=B*i;
    ll r=A-x%A;
    r%=A;
    ll z=x+r;

    if(i+r>z/A) ans=min(ans,z);
  }

  if(ans==INF) cout<<-1<<endl;
  else cout<<ans<<endl;

  return 0;
}

