def main(): import sys M, D, N, B = map(int, sys.stdin.read().split()) if N == 0: result = M % B print(result if result < 10 else 'A') return D_mod = D % B next_state = [] for i in range(B): a = (i + D_mod) % B if i == 0: res = 1 % B if a == 0 else 1 % B else: if a == 0: res = 0 % B else: res = pow(a, i, B) next_state.append(res) current = M % B history = [current] for _ in range(B): current = next_state[current] if current in history: idx = history.index(current) pre = history[:idx] cycle = history[idx:] break history.append(current) else: pre = history cycle = [] if N < len(history): answer = history[N] else: len_pre = len(pre) len_cycle = len(cycle) remaining = N - len_pre if len_cycle == 0: answer = history[-1] else: answer = cycle[remaining % len_cycle] if answer < 10: print(answer) else: print('A') if __name__ == "__main__": main()