#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CountingSeries { public:
	long long countThem(long long a, long long b, long long c, long long d, long long upperBound)
	{
		LL cnt = a<=upperBound ? (upperBound-a)/b+1 : 0;
		for(; c<=upperBound; c*=d) {
			if( !in_arith(a, b, c) )
				++cnt;
			if( d == 1 )
				break;
		}
		return cnt;
	}

	bool in_arith(LL a, LL b, LL v) const
	{
		return v-a>=0 && (v-a)%b==0;
	}
};

// 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 long long& Expected, const long long& 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(_, CountingSeries().countThem(a, b, c, d, upperBound));}
int main(){

CASE(0)
	long long a = 1LL; 
	long long b = 1LL; 
	long long c = 1LL; 
	long long d = 2LL; 
	long long upperBound = 1000LL; 
	long long _ = 1000LL; 
END
CASE(1)
	long long a = 3LL; 
	long long b = 3LL; 
	long long c = 1LL; 
	long long d = 2LL; 
	long long upperBound = 1000LL; 
	long long _ = 343LL; 
END
CASE(2)
	long long a = 40LL; 
	long long b = 77LL; 
	long long c = 40LL; 
	long long d = 100000LL; 
	long long upperBound = 40LL; 
	long long _ = 1LL; 
END
CASE(3)
	long long a = 452LL; 
	long long b = 24LL; 
	long long c = 4LL; 
	long long d = 5LL; 
	long long upperBound = 600LL; 
	long long _ = 10LL; 
END
CASE(4)
	long long a = 234LL; 
	long long b = 24LL; 
	long long c = 377LL; 
	long long d = 1LL; 
	long long upperBound = 10000LL; 
	long long _ = 408LL; 
END
CASE(5)
	long long a = 8LL; 
	long long b = 8LL; 
	long long c = 2LL; 
	long long d = 2LL; 
	long long upperBound = 80LL; 
	long long _ = 12LL; 
END
CASE(6)
	long long a = 1LL; 
	long long b = 12345LL; 
	long long c = 1LL; 
	long long d = 100000LL; 
	long long upperBound = 1000000000000LL; 
	long long _ = -1LL; 
END
}
// END CUT HERE
