T = int(input()) X,A = map(int,input().split()) Y,B = map(int,input().split()) S = set() if T>=0: ans = (T//A)*(X-A)+T n = T//A+1 while True: m = (n*A-T+B-1)//B if T-n*A+m*B in S:break ans = min(ans,T-n*(A-X)+m*(Y+B)) S.add(T-n*A+m*B) n += 1 else: T = abs(T) m = (T+B-1)//B ans = m*(Y+B)-T S.add(m*B-T) n = 1 while True: m = (n*A+T+B-1)//B if m*B-n*A-T in S:break ans = min(ans,m*(Y+B)-n*(A-X)-T) S.add(m*B-n*A-T) n += 1 print(ans)