#include <bits/stdc++.h>
#define mem(arr, val) memset(arr, val, sizeof(arr))
#define all(v) v.begin(), v.end()
#define endl '\n'
#define decision (yes ? "YES" : "NO")
using namespace std;
// const double PI = acos(-1.0);
// const double E = exp(1.0);
const double EPS = 1e-9;
const int INF = (int)2e9;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5;
const int N = (int)1e5;

int main() {
#ifdef MY_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<int> a(n), p(n), x(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> p[i] >> x[i];
    }
    vector<int> prices;
    for (int i = 0; i < n; i++) {
        if (a[i] < x[i]) {
            prices.push_back(p[i]);
        }
    }

    int ans = -1;
    if (!prices.empty()) ans = *min_element(all(prices));
    cout << ans << endl;

    return 0;
}

// clang-format off
/* #pragma GCC optimize("Ofast") */
/* #pragma GCC optimize("unroll-loops") */

#pragma GCC diagnostic ignored "-Wregister"

#include <bits/stdc++.h>
using namespace std;

#define int ll

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vpii;

namespace fio {
	struct fastio
	{
		char s[100000];
		int it,len;
		fastio(){it=len=0;}
		inline char get()
		{
			if(it<len)return s[it++];
			it=0;
			len=fread(s,1,100000,stdin);
			if(len==0)return EOF;else return s[it++];
		}
		bool notend()
		{
			char c=get();
			while(c==' '||c=='\n')c=get();
			if(it>0)it--;
			return c!=EOF;
		}
	}_buff;
	inline ll getnum()
	{
		ll r=0;bool ng=0;char c;c=_buff.get();
		while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
		if(c=='-')ng=1,c=_buff.get();
		while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
		return ng?-r:r;
	}
	template<class T> inline void write(T x) {
		if(x<0)putchar('-'),x=-x;
		register short a[20]={},sz=0;
		while(x)a[sz++]=x%10,x/=10;
		if(sz==0)putchar('0');
		for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
	}

	inline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}

	template<class T> inline void write(T x, char c) { putnum(x), putchar(c); }
	template<class T> inline void writeln(T x) { write(x), putchar('\n'); }
	template<class T> inline void writesp(T x) { write(x), putchar(' '); }

	void read() { ; }
	void read(int32_t &x) { x = getnum(); }
	void read(ll &x) { x = getnum(); }
	void read(string &s) { char c=_buff.get(); while(isspace(c))c=_buff.get(); while(not isspace(c)){ s += c; c=_buff.get(); } }
	template<typename H, typename... T> inline void read(H &x, T &... g) { read(x); read(g...); }
}
using namespace fio;

#define endl '\n'
#define rep(i, begin, end) for (__typeof(begin) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); (begin) > (end) ? i-- : i++)
#define all(X) begin(X),end(X)
#define sz(x) ((int) x.size())
// clang-format on

double eps = 1e-10;

int32_t main() {

#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	/* freopen("output.txt", "w", stdout); */
#endif
	int x, y, a, b;
	read(x, y, a, b);

	vi v;
	v.push_back(x);

	while (v.back() <= y / a)
		v.push_back(v.back() * a);

	if (v.back() >= y)
		v.pop_back();

	int res = sz(v) - 1;

	rep(i,0,sz(v))
		res = max(res, i + (y - v[i] - 1) / b);

	writeln(res);

	return 0;
}
