#pragma optimize GCC("-O3","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=105,INF=1<<30;

int main(){
    int A,B;cin>>A>>B;

    if(A==2) cout<<-1<<endl;
    else if(A==3){
        if(B==4) cout<<6<<endl;
        else cout<<-1<<endl;
    }else if(A==4){
        if(B==5||B==6) cout<<8<<endl;
        else if(B==9) cout<<12<<endl;
        else cout<<-1<<endl;
    }else{
    int m;
    for(int i=B;;i++){
        if(i%A==0){
            m=i;
            break;
        }
    }
    bool ok=false;

    for(int i=m;i<=2*B;i+=A){
        int a=(i/B)+(i%B)/A+(i%B)%A;
        int b=(i/A)+(i%A);
        if(a>b){
            cout<<i<<endl;
            ok=true;
            break;
        }
    }

    if(!ok) cout<<-1<<endl;
    }
    
}



