#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;


long long cmmdc(long long a, long long b) {
    long long r = a % b;

    while(r) {
        a = b;
        b = r;
        r = a % b;
    }

    return b;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif // ONLINE_JUDGE

    long long a, b;

    cin >> a >> b;

    long long auxb = b;

    while(b > a && cmmdc(a, b) > 1) {
        --b;
    }

    long long mx = b - a + 1;

    b = auxb;

    while(a < b && cmmdc(a, b) > 1) {
        ++a;
    }

    mx = max(mx, b - a + 1);

    if(mx == 1) {
        cout << -1 << '\n';
    }

    cout << mx << '\n';

    return 0;
}