import sys import heapq def main(): N, M, P = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) decomposed = [] has_b_gt1 = False for a in A: k = 0 while a % P == 0: a //= P k += 1 b = a decomposed.append((k, b)) if b > 1: has_b_gt1 = True if not has_b_gt1: # Check if any P^k_i > M found = False for k, b in decomposed: current = 1 for _ in range(k): current *= P if current > M: break if current > M: found = True break print(1 if found else -1) return # BFS with priority queue heap = [] heapq.heappush(heap, (0, 1)) visited = {1: 0} answer = -1 while heap: steps, r = heapq.heappop(heap) if answer != -1 and steps >= answer: continue if visited.get(r, float('inf')) < steps: continue for k, b in decomposed: new_r = r * b X = new_r * (P ** k) if X > M: if answer == -1 or steps + 1 < answer: answer = steps + 1 continue # Compute s_min s = 0 current = new_r while current <= M and s <= k: current *= P s += 1 if s <= k: total_steps = steps + 1 + (k - s) if answer == -1 or total_steps < answer: answer = total_steps else: if new_r > M: total_steps = steps + 1 + k if answer == -1 or total_steps < answer: answer = total_steps else: if new_r not in visited or steps + 1 + k < visited.get(new_r, float('inf')): visited[new_r] = steps + 1 + k heapq.heappush(heap, (steps + 1 + k, new_r)) print(answer if answer != -1 else -1) if __name__ == "__main__": main()