## https://yukicoder.me/problems/no/2099 def main(): T = int(input()) X, A = map(int, input().split()) Y, B = map(int, input().split()) # yが取りうる最小値を取得 if T >= 0: y_min = 0 else: y_min = (- T) // B + (1 if (-T) % B > 0 else 0) # 本処理 min_answer = float("inf") t = (T + y_min * B) % A cost = y_min # 一発目 xx = T + cost * B answer = cost * Y + t xx0 = xx - t a = xx0 // A answer += a * X min_answer = min(min_answer, answer) first = t cost += 1 t += B t %= A while t != first: xx = T + cost * B answer = cost * Y + t xx0 = xx - t a = xx0 // A answer += a * X min_answer = min(min_answer, answer) cost += 1 t += B t %= A print(min_answer) if __name__ == "__main__": main()