N = int(input()) P, Q, R = map(int, input().split()) A, B, C = map(int, input().split()) x = -1 for k in range(Q): now = A + P * k if now % Q == B: x = now break y = -1 period_pq = P * Q for k in range(R): now = x + period_pq * k if now % R == C: y = now break period = P * Q * R if y > N: print(0) else: print((N - y) // period + 1)