#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define mod 1000000007
#define M 1000000007
#define pb push_back
#define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mx 1000005
#define fiint(a,b) memset(a,b,sizeof(a))
#define bitcount __builtin_popcount
#define fori(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cout<<x<<endl;
#define cases(t) int t; cin>>t; while(t--)
#define inf1 INT_MAX
#define all(a) a.begin(),a.end()
#define vec vector<int>
#define pii pair<int,int>
#define plii pair<int,int>
#define pint pair<int,int>
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mp make_pair
 
#define sz(x) (int)x.size()
#define PI    3.14159265359

const long long INF=1e18;

bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }

 
int factorial[10000001]={0};
 
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}


int powerFunction(int x,int y){
    int res = 1;
     int p = mod;
    x = x;
    while (y > 0){
        if (y & 1)
            res = (res*x)%p ;
        y = y>>1;
        x = (x*x)%p ;
    }
    return res;
}

void factorialFunction(int maxLimit){
    factorial[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
    factorial[i]=(factorial[i-1]*i)%MOD;
    return;
}
 
int nCr(int n, int r)
{
    if(r < 0 || r > n){
        return 0;
    }
    int temp = factorial[n];
    temp *= (powerFunction(factorial[r], MOD-2)%MOD);
    temp %= MOD;
    temp *= (powerFunction(factorial[n-r], MOD-2)%MOD);
    temp %= MOD;
    return temp;
}

map<int,int> dp;

int find(int x ,int y )
{
    if(x >= y)
        return x - y;
    
    if(dp.find(y) != dp.end())
        return dp[y];
    
    if(y % 2 == 0 )
    {
        int a = find(x , y/2 ) + 1;
        dp[y] = min(y - x , a);
        return dp[y];
    }
    else
    {
        int a = find( x , (y+1)/2 ) + 2;
        int b = find(x , (y-1)/2) + 2;
        dp[y] =  min(min(a,b) , y - x );
        return dp[y];
    }
}


signed main()
{
    int x,y;
    cin>>x>>y;
    cout<< find(x,y)<<endl;
}
    



#include<cstdio>
#include<cctype>
#include<algorithm>
#define LL long long
using namespace std;

template <class I>
inline void read(I &z)
{
    z=0;
    char c=getchar();int base=1;
    while (!isdigit(c) && c!='-') c=getchar();
    if (c=='-') c=getchar(),base=-1;
    while (isdigit(c)) z=z*10+c-'0',c=getchar();
    z*=base;
}

int x,y,z,n,ans=2147483647;

int main(int argc, char const *argv[])
{
    read(n);
    for (int i=1;i<=n;++i)
    {
        read(x),read(y),read(z);
        if (x<z) ans=min(ans,y);
    }
    if (ans==2147483647) printf("-1"); else printf("%d",ans);
    return 0;
}
