Problem:
Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const ll BIG=1e15;

int par[100001];
int rnk[100001];
int size[100001];
int N;

void shoki(){
    for(int i=0;i<=N;i++) {par[i]=i; rnk[i]=0; size[i]=1;}
}

int sch(int x){
    if(par[x]==x) return x;
    else return par[x]=sch(par[x]);
}

void con(int A,int B){
    A=sch(A);
    B=sch(B);
    if(A==B) return;
    if(rnk[A]>rnk[B]) swap(A,B);
    par[A]=B;
    size[B]+=size[A];
    size[A]=size[B];
    if(rnk[A]==rnk[B]) rnk[B]++;

    return;
}

int is_size(int k){
    return size[sch(k)];
}

bool same(int A,int B){
    return sch(A)==sch(B);
}

int main(){
    cin>>N;
    int M; cin>>M;
   
    shoki();
    vector<ll> a(N);
    if(N==1) {
        cout<<0<<endl;
        //exit(1);
    }
    else{
    for(int i=0;i<N;i++) cin>>a.at(i);
    for(int i=0;i<M;i++){
        int x,y; cin>>x>>y;
        con(x,y);
    }

    vector<int> heads;

    ll sum=0;
    for(int i=0;i<N;i++){
        if(sch(i)==i) {
            sum+=(size[i]-2);
            heads.push_back(i);
        }
    }
    sum+=2;
    if(sum<0) {
        cout<<"Impossible"<<endl;
        //exit(1);
    }
    else{
    int H=heads.size();
    vector<vector<ll>> A(H);

    sort(heads.begin(),heads.end());
    for(int i=0;i<N;i++){
        auto itr=find(heads.begin(),heads.end(),sch(i));
        int x=itr-heads.begin();
        A[x].push_back(a.at(i));
    }

    ll ans=0;
    vector<ll> kahen;

    for(int i=0;i<H;i++){
        sort(A[i].begin(),A[i].end());
        ans+=A[i].at(0);
        if(A[i].size()>1) {
            for(int j=1;j<A[i].size();j++) kahen.push_back(A[i].at(j));
        }
    }

    sort(kahen.begin(),kahen.end());
    for(int i=0;i<kahen.size()-sum;i++) ans+=kahen.at(i);
    cout<<ans<<endl;

    
    



    


    }
}
}