#define _USE_MAT_DEFINES
#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <stack>
#include <deque>
#include <numeric>
#include <functional>
using namespace std;

typedef long long ll;

#define rep(i,n) for(long long (i)=0;(i)<(n);(i)++)
#define FOR(i, m, n) for(long long (i) = (m);(i) < (n);(i)++)

#define INF 1e9
const long long MOD = 1000000007;//1e9+7

int main() {
    ll X, Y, A, B;
    cin >> X >> Y >> A >> B;

    ll ans = 0;
    while ((double)A*X <= 2e18 &&X*A<X+B&&X*A<Y) {
        X *= A;
        ans++;
    }
    ans += (Y - 1 - X) / B;
    cout << ans;

    return 0;
}
#include<bits/stdc++.h>

typedef long long int lli;
typedef long double lld;
typedef long long ll;

//Fast Input I/O
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL);
//Datatype
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define vvlli vector<vector<long long int>>
#define ppi pair<int, int>
#define rppi pair<int, pair<int, int>>
#define lppi pair<pair<lli, lli>, lli>
#define vppi vector<pair<int, int>>
#define sppi stack<pair<int int>>
#define qppi queue<pair<int, int>>
//function
#define f first
#define s second
#define pb(x) push_back(x)
#define mkp(i, j) make_pair(i, j)
#define lmkp(i,j,k) make_pair(make_pair(i,j),k)
#define rmkp(i,j,k) make_pair(i,make_pair(j,k))
//loop
#define loop(i,n) for (i = 0; i < n; ++i)
#define loops(i,k,n) for (i = k; i <= n; ++i)
#define looprev(i,k,n) for (i = k; i >= n; --i)
//Const
#define inf (int)1e9
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//Print
#define prd(n) printf("%d", n)
#define prl(n) printf("%lld", n)
#define prdn(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define prf(n) printf("%f", n)
//Scan
#define scd(n) scanf("%d", &n)
#define scd2(a, b) scanf("%d %d", &a, &b)
#define scd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scl(n) scanf("%lld", &n)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define scf(n) scanf("%f", &n)
#define all(x) (x).begin(), (x).end()

using namespace std;
void online_judge() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}


void solve() {
	lli i, j, k, n;
	cin >> n;
	vector<lppi> A;
	loop(i, n) {
		lli t, l, r;
		cin >> t >> l >> r;
		A.push_back(lmkp(l, r, t));
	}
	sort(A.begin(), A.end());
	lli res = 0;
	loop(i, n) {
		lli l = A[i].f.f, r = A[i].f.s, t = A[i].s;
		loops(j, i + 1, n - 1) {
			if (A[j].f.f < r || (A[j].f.f == r && (t == 1 || t == 3) && (A[j].s == 1 || A[j].s == 2))) {
				res += 1;
			}
		}
	}
	cout << res << "\n";
}

int main() {
	fio;
	online_judge();
	lli t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}