#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pii pair<int,int>
#define tii tuple <int,int,int>
#define N 200005
#define mod 1000000005
#define X first
#define Y second
#define eps 0.0000000001
#define all(x) x.begin(),x.end()
#define tot(x) x+1,x+n+1
using namespace std;
inline bool ok(ll a, ll b) {
    ll r = 0;

    while(b) {
        r = a - (a / b)*b;
        a = b;
        b = r;
    }

    if(a == 1)
        return 1;

    return 0;
}
ll a, b, l, i, aa, bb, k;
int main() {
    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);
    cin >> a >> b;

    for(l = 0;; l++) {
        k = 0;

        for(i = 0; i <= l; i++) {
            aa = a + i;
            bb = b - (l - i);

            if(aa >= bb)
                ;
            else
                k = 1;

            if(ok(aa, bb)) {
                cout << bb - aa + 1;
                return 0;
            }
        }

        if(!k)
            break;
    }

    cout << -1;
    return 0;
}