# https://ikatakos.com/pot/programming_algorithm/number_theory/euclidean_algorithm # 引用(もといパクリ) # Python で RSA 公開鍵暗号をなぞってみる - CAMPHOR- Tech Blog # https://tech.camph.net/rsa-public-key-encryption/ def ex_euclid(x, y): c0, c1 = x, y a0, a1 = 1, 0 b0, b1 = 0, 1 while c1 != 0: m = c0 % c1 q = c0 // c1 c0, c1 = c1, m a0, a1 = a1, (a0 - q * a1) b0, b1 = b1, (b0 - q * b1) return c0, a0, b0 def exex_euclid(x,y,z): c, a, b = ex_euclid(x, y) w, m = divmod(z, c) # zがcの倍数でないなら等式は不可能 if m != 0: return None u, v = x // c, y // c a, b = a * w, b * w # aを非負数の中で最小にする f, a = divmod(a, v) b += u * f # aを最小にしたのにbが負なら、ともに正の組は不可能 if b < 0: return None return c, a, b, u, v import sys input = sys.stdin.readline N = int(input()) P, Q, R = map(int, input().split()) A, B, C = map(int, input().split()) c, i, j, u, v = exex_euclid(P, -Q, B-A) #print(c, i, j, u, v) x = P*i+A m = P*Q while x%R!=C: x += m #print(x) t = P*Q*R ans = (N-x)//t + 1 print(ans)