Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants.


-----Input-----

The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b < p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime.


-----Output-----

Print a single integer: the number of possible answers $n$.


-----Examples-----
Input
2 3 5 8

Output
2

Input
4 6 7 13

Output
1

Input
233 233 10007 1

Output
1



-----Note-----

In the first sample, we can see that $n=2$ and $n=8$ are possible answers.

Difficulty: HARD

Solutions:
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())

def main():
	(a, b, p, x) = mpint()
	ans = 0
	by = b
	for j in reversed(range(p - 1)):
		by = by * a % p
		i = (j - by) % p
		if i > (x - j) // (p - 1):
			continue
		ans += 1 + ((x - j) // (p - 1) - i) // p
	print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
else:
	if DEBUG != 3:
		(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	if DEBUG:
		_print = print

		def print(*args, **kwargs):
			_print(*args, **kwargs)
			sys.stdout.flush()
	main()


import sys

def calc(a, b, p, x):
	ans = 0
	power = pow(a, p - 2, p)
	(a2, b2, s, d1) = (1, b, 0, -b)
	prod = p * (p - 1)
	while s < p - 1:
		k = (d1 + s) % p
		L = x - s - (p - 1) * k
		ans += L // prod + 1
		d1 = d1 * power % p
		s += 1
	return str(ans)
(a, b, p, x) = list(map(int, sys.stdin.readline().split()))
sys.stdout.write(calc(a, b, p, x))


def AC():
	(a, b, p, x) = map(int, input().split())
	ans = 0
	m = 1
	for i in range(p - 2):
		m = m * a % p
	rem = 1
	inv = 1
	Ch = p * (p - 1)
	for n in range(1, p):
		rem = rem * a % p
		inv = inv * m % p
		cur = min(p, ((n * rem - b) * inv + p) % p)
		rep = n + cur * (p - 1)
		ans += max(0, (x - rep + Ch) // Ch)
	print(ans)
AC()


(a, b, p, x) = [int(x) for x in input().split()]
powers = [a]
while powers[-1] != 1:
	powers.append(powers[-1] * a % p)
order = len(powers)
powers = [powers[-1]] + powers
inverse = pow(order, p - 2, p)

def f(sol):
	return x // (p * order) + (1 if sol <= x % (p * order) else 0)

def chinese(a, n, b, m):
	k = inverse * (b - a + m) % m
	x = k * n + a
	return x % (n * m)
res = 0
for i in range(len(powers) - 1):
	inv = powers[order - i]
	val = inv * b % p
	sol = chinese(i, order, val, p)
	res += f(sol)
print(res)