
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e18;
int inf = 1e9;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    ll x, y; cin >> x >> y;

    if (x >= y) {
        print(x - y);
        return 0;
    }

    unordered_map<ll, ll> m;

    ll ans = INF;

    auto dfs = [&] (auto && f, ll y_cur, int cnt) {
        if (ans <= cnt) return;

        auto it = m.find(y_cur);
        if (it != m.end()) {
            if (it->second <= cnt) return;
        }

        m[y_cur] = cnt;

        ans = min(ans, cnt + abs(x - y_cur));

        if (y_cur <= x) return;

        if (y_cur & 1) {
            f(f, (y_cur + 1), cnt + 1);
            f(f, (y_cur - 1), cnt + 1);
        } else {
            f(f, y_cur / 2, cnt + 1);
        }
    };

    dfs(dfs, y, 0);

    print(ans);
}

/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include<bits/stdc++.h>
#define ALL(x) (x).begin(),(x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int a[maxn],p[maxn],x[maxn];

int main()
{
    fastio();
    int n; cin >> n;
    rep(i,1,n) cin >> a[i] >> p[i] >> x[i];
    int ans = inf;
    rep(i,1,n){
        if(a[i] <= (x[i]-1) ) ans = min(ans,p[i]);
    }
    if(ans==inf) cout << -1 << endl;
    else cout <<ans<<endl;
    return 0;
}
