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