#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CatchTheBeat { public:
	int maxCatched(int n, int x0, int y0, int a, int b, int c, int d, int mod1, int mod2, int offset)
	{
		vector<int> x(n), y(n);
		x[0] = x0;
		for(int i=1; i<n; ++i)
			x[i] = int((LL(x[i-1]) * a + b) % mod1);
		for(int i=0; i<n; ++i)
			x[i] = x[i] - offset;

		y[0] = y0;
		for(int i=1; i<n; ++i)
		    y[i] = int((LL(y[i-1]) * c + d) % mod2);

		return solve(n, x, y);
	}

	int solve(int N, const vector<int>& x, const vector<int>& y)
	{
		vector<pair<int,int>> tx;
		for(int i=0; i<N; ++i)
			tx.emplace_back(y[i], x[i]);
		tx.emplace_back(0, 0);
		sort(tx.begin(), tx.end());

		vector<vector<int>> score_table(tx.size());
		score_table[0].push_back(0);
		int best_score = 0;
		for(int c=1; c<tx.size(); ++c)
		{
			int pre = -1;
			for(int bs=best_score; bs>=0; --bs) { 
				for(int p: score_table[bs])
					if(tx[c].first - tx[p].first >=  abs(tx[c].second - tx[p].second))
						{pre = bs; goto found_pre;}
			}
		found_pre:
			if(pre>=0) {
				score_table[pre+1].push_back(c);
				best_score = max(best_score, pre+1);
			}
		}
		return best_score;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CatchTheBeat().maxCatched(n, x0, y0, a, b, c, d, mod1, mod2, offset));}
int main(){

CASE(0)
	int n = 3; 
	int x0 = 0; 
	int y0 = 0; 
	int a = 1; 
	int b = 1; 
	int c = 1; 
	int d = 1; 
	int mod1 = 100; 
	int mod2 = 100; 
	int offset = 1; 
	int _ = 2; 
END
CASE(1)
	int n = 1; 
	int x0 = 0; 
	int y0 = 1234; 
	int a = 0; 
	int b = 0; 
	int c = 0; 
	int d = 0; 
	int mod1 = 1000000000; 
	int mod2 = 1000000000; 
	int offset = 1000; 
	int _ = 1; 
END
CASE(2)
	int n = 1; 
	int x0 = 0; 
	int y0 = 999; 
	int a = 0; 
	int b = 0; 
	int c = 0; 
	int d = 0; 
	int mod1 = 1000000000; 
	int mod2 = 1000000000; 
	int offset = 1000; 
	int _ = 0; 
END
CASE(3)
	int n = 100; 
	int x0 = 0; 
	int y0 = 0; 
	int a = 1; 
	int b = 1; 
	int c = 1; 
	int d = 1; 
	int mod1 = 3; 
	int mod2 = 58585858; 
	int offset = 1; 
	int _ = 66; 
END
CASE(4)
	int n = 500000; 
	int x0 = 123456; 
	int y0 = 0; 
	int a = 1; 
	int b = 0; 
	int c = 1; 
	int d = 1; 
	int mod1 = 1000000000; 
	int mod2 = 1000000000; 
	int offset = 0; 
	int _ = 376544; 
END
CASE(5)
	int n = 500000; 
	int x0 = 0; 
	int y0 = 0; 
	int a = 0; 
	int b = 0; 
	int c = 0; 
	int d = 0; 
	int mod1 = 1; 
	int mod2 = 1; 
	int offset = 0; 
	int _ = 500000; 
END
CASE(6)
	int n = 10; 
	int x0 = 999999957; 
	int y0 = 79; 
	int a = 993948167; 
	int b = 24597383; 
	int c = 212151897; 
	int d = 999940854; 
	int mod1 = 999999986; 
	int mod2 = 999940855; 
	int offset = 3404; 
	int _ = 3; 
END
/*
CASE(7)
	int n = ; 
	int x0 = ; 
	int y0 = ; 
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int mod1 = ; 
	int mod2 = ; 
	int offset = ; 
	int _ = ; 
END
CASE(8)
	int n = ; 
	int x0 = ; 
	int y0 = ; 
	int a = ; 
	int b = ; 
	int c = ; 
	int d = ; 
	int mod1 = ; 
	int mod2 = ; 
	int offset = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
