PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000288. Hai cấp số cộng (Two Arithmetic Progressions)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hai cấp số cộng (Two Arithmetic Progressions)

Two Arithmetic Progressions

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two infinite arithmetic progressions, whose terms are a1k+b1a_1k+b_1 and a2t+b2a_2t+b_2, where k,tk,t are nonnegative integers. Both common differences a1,a2a_1,a_2 are positive, while the initial terms b1,b2b_1,b_2 may be negative.

Given an integer interval [L,R][L,R], count the distinct integers xx in this interval which appear in both progressions. Count each integer once.

Input

A single line contains six integers in this order: a1,b1,a2,b2,L,Ra_1,b_1,a_2,b_2,L,R.

Output

Print a single integer: the number of qualifying values xx.

Subtasks

  • Subtask 1 (20 points): 1≤a1,a2≤201\le a_1,a_2\le 20; −50≤b1,b2,L,R≤50-50\le b_1,b_2,L,R\le50; L≤RL\le R.
  • Subtask 2 (30 points): 1≤a1,a2≤1051\le a_1,a_2\le 10^5; −106≤b1,b2,L,R≤106-10^6\le b_1,b_2,L,R\le10^6; L≤RL\le R.
  • Subtask 3 (50 points): 1≤a1,a2≤2⋅1091\le a_1,a_2\le 2\cdot10^9; −2⋅109≤b1,b2,L,R≤2⋅109-2\cdot10^9\le b_1,b_2,L,R\le2\cdot10^9; L≤RL\le R.

Examples

Input

2 0 3 3 5 21

Output

3

Explanation

The common values in the interval are 6, 12, and 18. For 6 the progression indices are 3 and 1; for 12 they are 6 and 3; for 18 they are 9 and 5. Exactly three distinct values qualify.

Input

2 4 3 0 6 17

Output

2

Explanation

The common values between 6 and 17 are 6 and 12. For 6 the indices are 1 and 2, and for 12 they are 4 and 4. No other number in the given interval belongs to both progressions.