#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 TheEmpireStrikesBack { public:
	int find(int AX, int BX, int CX, int AY, int BY, int CY, int N, int M)
	{
		vector<pair<int,int>> P(N);
		P[0].first = AX;
		P[0].second = AY;
		for(int i=1; i<N; ++i) {
			P[i].first = int((LL(P[i-1].first) * BX + CX) % 1000000007);
			P[i].second = int((LL(P[i-1].second) * BY + CY) % 1000000007);
		}

		// Sort: Large Y first, Large X first
		sort(P.begin(), P.end(), [&](const pair<int,int>& lhs, const pair<int,int>& rhs){
			if(lhs.second == rhs.second) return lhs.first > rhs.first;
			return lhs.second > rhs.second;
		});

		// Border stars, worth targetting.
		vector<pair<int,int>> B;
		for(auto& p: P)
			if(B.empty() || B.back().first < p.first)
				B.push_back(p);

		int L=-1, R=1000000007; // (L,R]
		while(R-L>1) {
			int C = (L+R)/2;
			(least_missile_to_destory_all(B, C)<=M ? R : L) = C;
		}
		return R;
	}

	int least_missile_to_destory_all(const vector<pair<int,int>>& B, int T)
	{
		// Greedy.
		int count = 0;
		for(int to_cover=0; to_cover<B.size(); ) {
			int missile = to_cover;
			for(; missile<B.size(); ++missile)
				if(B[missile].first+T < B[to_cover].first || B[missile].second+T < B[to_cover].second)
					break;
			--missile;
			++count;
			for(; to_cover<B.size(); ++to_cover)
				if(B[missile].first+T < B[to_cover].first || B[missile].second+T < B[to_cover].second)
					break;
		}
		return count;
	}
};

// 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(_, TheEmpireStrikesBack().find(AX, BX, CX, AY, BY, CY, N, M));}
int main(){

CASE(0)
	int AX = 2; 
	int BX = 2; 
	int CX = 2; 
	int AY = 2; 
	int BY = 2; 
	int CY = 2; 
	int N = 2; 
	int M = 1; 
	int _ = 0; 
END
CASE(1)
	int AX = 2; 
	int BX = 2; 
	int CX = 2; 
	int AY = 2; 
	int BY = 4; 
	int CY = 1000000000; 
	int N = 2; 
	int M = 1; 
	int _ = 1; 
END
CASE(2)
	int AX = 1; 
	int BX = 3; 
	int CX = 8; 
	int AY = 10000; 
	int BY = 10; 
	int CY = 999910000; 
	int N = 3; 
	int M = 1; 
	int _ = 30; 
END
CASE(3)
	int AX = 0; 
	int BX = 0; 
	int CX = 0; 
	int AY = 0; 
	int BY = 0; 
	int CY = 0; 
	int N = 100000; 
	int M = 1000; 
	int _ = 0; 
END
CASE(4)
	int AX = 10; 
	int BX = 20; 
	int CX = 30; 
	int AY = 40; 
	int BY = 50; 
	int CY = 60; 
	int N = 100000; 
	int M = 10; 
	int _ = 15720; 
END
/*
CASE(5)
	int AX = ; 
	int BX = ; 
	int CX = ; 
	int AY = ; 
	int BY = ; 
	int CY = ; 
	int N = ; 
	int M = ; 
	int _ = ; 
END
CASE(6)
	int AX = ; 
	int BX = ; 
	int CX = ; 
	int AY = ; 
	int BY = ; 
	int CY = ; 
	int N = ; 
	int M = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
