import heapq def dijkstra(s, n, edge): dist = [float("Inf")]*n dist[s] = 0 hq = [[0,s]] heapq.heapify(hq) while len(hq) > 0: d,i = heapq.heappop(hq) if dist[i] < d: continue for j,d_1 in edge[i]: if dist[j] > (dist[i] + d_1): dist[j] = dist[i] + d_1 heapq.heappush(hq, [dist[j],j]) return dist N,A,B,C = map(int,input().split()) fact = [1] for i in range(1,N+1): f = fact[-1]*i f %= N fact.append(f) G = [[] for i in range(2*N)] for x in range(1,N): G[N+x].append((0,C)) G[N].append((0,0)) for x in range(1,2*N): G[x].append(((x+1) % (2*N),A)) for x in range(2,N): f = 0 for k in range(2,41): if f == 0: xx = pow(x,k) if xx >= N: f = 1 else: xx = pow(x,k,N) if pow(B,k) >= A*N: break if xx % N == 0: G[x].append((0,pow(B,k))) G[x+N].append((0,pow(B,k))) break if f: G[x].append((N+(xx%N),pow(B,k))) else: G[x].append((xx,pow(B,k))) G[x+N].append((N + xx % N,pow(B,k))) if fact[x] == 0: G[x].append((0,C)) continue if x >= 9: G[x].append((N + fact[x],C)) else: f = 1 for i in range(1,x+1): f *= i if f > N: G[x].append((N + (f % N),C)) else: G[x].append((f,C)) dist = dijkstra(1, 2*N, G) print(dist[0])