#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
long long gcd(long long a, long long b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main(){
    long long a,b;
    long long k,n;
    cin >> a >> b;
    k = a;
    while(k < b){
    n = b;
    while(gcd(k,n) != 1 && n > k){
        --n;
    }
    if(k != n)
    break;
    ++k;
    }
    if(k != n){
    cout << n-k+1;
    }else{
    cout << -1;
    }
    return 0;
}