Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days $[l_a; r_a]$ are lucky, then there are some unlucky days: $[r_a + 1; l_a + t_a - 1]$, and then there are lucky days again: $[l_a + t_a; r_a + t_a]$ and so on. In other words, the day is lucky for Alice if it lies in the segment $[l_a + k t_a; r_a + k t_a]$ for some non-negative integer $k$.

The Bob's lucky day have similar structure, however the parameters of his sequence are different: $l_b$, $r_b$, $t_b$. So a day is a lucky for Bob if it lies in a segment $[l_b + k t_b; r_b + k t_b]$, for some non-negative integer $k$.

Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.


-----Input-----

The first line contains three integers $l_a$, $r_a$, $t_a$ ($0 \le l_a \le r_a \le t_a - 1, 2 \le t_a \le 10^9$) and describes Alice's lucky days.

The second line contains three integers $l_b$, $r_b$, $t_b$ ($0 \le l_b \le r_b \le t_b - 1, 2 \le t_b \le 10^9$) and describes Bob's lucky days.

It is guaranteed that both Alice and Bob have some unlucky days.


-----Output-----

Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.


-----Examples-----
Input
0 2 5
1 3 5

Output
2

Input
0 1 3
2 3 6

Output
1



-----Note-----

The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.

[Image]

[Image]

Difficulty: HARD

Solutions:
def inters(x1, y1, x2, y2):
	return max(0, min(y1, y2) - max(x1, x2) + 1)
from math import gcd
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
shift = gcd(ta, tb)
start = la - la % shift
la -= start
ra -= start
start = lb - lb % shift
lb -= start
rb -= start
res = max(inters(la, ra, lb + shift, rb + shift), inters(la + shift, ra + shift, lb, rb), inters(la, ra, lb, rb))
print(res)


import math
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
g = math.gcd(ta, tb)
ra -= la
t = rb - lb
lb -= la
lb %= g
rb = lb + t
c = min(rb, ra) - lb + 1
n = min(rb, ra + g) - g + 1
print(max(c, n, 0))


import math
(a, b, c) = map(int, input().split())
(x, y, z) = map(int, input().split())
d = math.gcd(c, z)
t = (x - a) // d
ans = max(0, min(b + t * d, y) - max(a + t * d, x) + 1)
t += 1
ans = max(ans, min(b + t * d, y) - max(a + t * d, x) + 1)
print(ans)


def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)
(la, ra, ta) = list(map(int, input().split()))
(lb, rb, tb) = list(map(int, input().split()))
gcd1 = gcd(ta, tb)
luca = ra - la + 1
lucb = rb - lb + 1
if (la - lb) % gcd1 == 0:
	print(min(luca, lucb))
else:
	op1 = lucb - (la - lb) % gcd1
	op2 = luca - (lb - la) % gcd1
	opf = max(op1, op2, 0)
	print(min(opf, luca, lucb))


def euclid(data):
	a = data[0]
	for b in data[1:]:
		if b < a:
			(a, b) = (b, a)
		while 1:
			r = b % a
			if r == 0:
				break
			else:
				b = a
				a = r
	return a
(la, ra, ta) = list(map(int, input().split()))
(lb, rb, tb) = list(map(int, input().split()))
if la < lb:
	(la, lb) = (lb, la)
	(ra, rb) = (rb, ra)
	(ta, tb) = (tb, ta)
gcd = euclid([ta, tb])
tmp = la - lb - gcd * ((la - lb) // gcd)
print(max([0, min([ra - la + tmp, rb - lb]) - tmp + 1, min([ra - la, rb - lb + gcd - tmp]) - (gcd - tmp) + 1]))


import math

def solve(la, ra, ta, lb, rb, tb):
	if la > lb:
		(la, ra, ta, lb, rb, tb) = (lb, rb, tb, la, ra, ta)
	da = ra - la
	db = rb - lb
	res = 0
	g = math.gcd(ta, tb)
	dist = (lb - la) % g
	if dist == 0:
		return min(da + 1, db + 1)
	(la, ra) = (lb - dist, lb - dist + da)
	res = max(res, 1 + min(rb, ra) - lb)
	res = max(res, 1 + min(rb, ra + g) - (la + g))
	return res
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
print(solve(la, ra, ta, lb, rb, tb))


from math import gcd
r = lambda : list(map(int, input().split()))
(a, b) = (r(), r())
c = b[0] > a[0]
(la, ra, ta) = a if c else b
(lb, rb, tb) = b if c else a
g = gcd(ta, tb)
(lna, lnb) = (ra - la + 1, rb - lb + 1)
d = lb - la - (lb - la) // g * g
print(max(0, min(lna - d, lnb), min(lna, lnb - g + d)))


(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
g = gcd(ta, tb)
l0 = la % g
l1 = lb % g
r0 = l0 + ra - la
r1 = l1 + rb - lb
res = 0
l = max(l0, l1)
r = min(r0, r1)
res = max(res, r - l + 1)
l = max(l0, l1 + g)
r = min(r0, r1 + g)
res = max(res, r - l + 1)
l = max(l0 + g, l1)
r = min(r0 + g, r1)
res = max(res, r - l + 1)
print(res)


from math import gcd
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
g = gcd(ta, tb)
ra -= la
temp = rb - lb
lb -= la
lb %= g
rb = lb + temp
current = min(rb, ra) - lb + 1
now = min(rb, ra + g) - g + 1
print(max(current, now, 0))


from math import gcd
(lA, rA, tA) = map(int, input().split())
(lB, rB, tB) = map(int, input().split())
rA += 1
rB += 1
move = gcd(tA, tB)
if lA > lB:
	(lA, rA, tA, lB, rB, tB) = (lB, rB, tB, lA, rA, tA)
d = (lB - lA) // move
lA += d * move
rA += d * move
first = min(rA, rB) - max(lA, lB)
lA += move
rA += move
second = min(rA, rB) - max(lA, lB)
print(max(0, first, second))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=-10 ** 6, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(50001)]
pp = []

def SieveOfEratosthenes(n=50000):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for i in range(50001):
		if prime[i]:
			pp.append(i)
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
s = set()
res = 0
g = math.gcd(ta, tb)
k = (la - lb) / g
klo = math.floor(k)
khi = math.ceil(k)

def intersection(ax, ay, bx, by):
	(l, r) = (max(ax, bx), min(ay, by))
	if l > r:
		return 0
	return r - l + 1
ans = 0
for dk in [klo, khi]:
	df = lb - la + dk * g
	bx = la + df
	by = bx + (rb - lb)
	ans = max(ans, intersection(la, ra, bx, by))
print(ans)


from math import gcd

def solve(la, ra, ta, lb, rb, tb):
	m = gcd(ta, tb)
	m1 = la % m
	k = (lb - m1 + m - 1) // m
	y = k * m + m1
	return max(0, min(ra - la + 1, rb - y + 1))
(la, ra, ta) = list(map(int, input().split()))
(lb, rb, tb) = list(map(int, input().split()))
print(max(solve(la, ra, ta, lb, rb, tb), solve(lb, rb, tb, la, ra, ta)))


import math

def main():
	(la, ra, ta) = [int(c) for c in input().split()]
	(lb, rb, tb) = [int(c) for c in input().split()]
	if lb < la:
		(la, lb) = (lb, la)
		(ra, rb) = (rb, ra)
		(ta, tb) = (tb, ta)
	gcd = math.gcd(ta, tb)
	(lena, lenb) = (ra - la + 1, rb - lb + 1)
	d = lb - la
	diff = d - d // gcd * gcd
	cur = lena - diff
	ans = max(0, min(cur, lenb))
	diff -= gcd
	diff = -diff
	cur = lenb - diff
	ans = max(ans, min(lena, cur))
	print(ans)
main()


import math
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
x = math.gcd(ta, tb)
d = la - lb
print(max(min(ra - la + 1, rb - lb + 1 - d % x), min(rb - lb + 1, ra - la + 1 - -d % x), 0))


import math

def solve():
	(l1, r1, t1) = list(map(int, input().split()))
	(l2, r2, t2) = list(map(int, input().split()))
	g = math.gcd(t1, t2)
	K1 = (l2 - l1) // g
	K2 = (l2 - l1) // g
	if (l2 - l1) % g != 0:
		K2 += 1
	diff1 = l1 - l2 + K1 * g
	diff2 = l1 - l2 + K2 * g
	if t1 == t2:
		print(min(r1, r2) - max(l1, l2) + 1)
		return
	ans = 0
	for i in [diff1, diff2]:
		l22 = l2 + i
		r22 = l22 + r1 - l1
		ans = max(ans, min(r22, r2) - max(l22, l2) + 1)
	print(ans)
	return
t = 1
while t > 0:
	t -= 1
	solve()


import sys
import string
from math import gcd
import getpass
import math
from decimal import Decimal
import pprint

def ria():
	return [int(i) for i in input().split()]
if getpass.getuser() != 'frohenk':
	filename = 'half'
else:
	sys.stdin = open('input.txt')
(la, ra, ta) = ria()
(lb, rb, tb) = ria()
if ta > tb:
	(la, ra, ta, lb, rb, tb) = (lb, rb, tb, la, ra, ta)
gc = gcd(tb, ta)
if gc == 1:
	print(min(ra - la + 1, rb - lb + 1))
	return

def get(st):
	nonlocal la, ra, ta, lb, rb, tb
	lc = la + st
	rc = ra + st
	return max(min(rc, rb) - max(lc, lb) + 1, 0)
sta = la // gc
stb = lb // gc
fna = ra // gc
fnb = rb // gc
mx = 0
mx = max(mx, get((stb - sta) * gc))
mx = max(mx, get((stb - sta + 1) * gc))
mx = max(mx, get((stb - sta + 2) * gc))
mx = max(mx, get((stb - sta - 1) * gc))
mx = max(mx, get((stb - sta - 2) * gc))
mx = max(mx, get((fnb - fna) * gc))
mx = max(mx, get((fnb - fna + 1) * gc))
mx = max(mx, get((fnb - fna + 2) * gc))
mx = max(mx, get((fnb - fna - 1) * gc))
mx = max(mx, get((fnb - fna - 2) * gc))
print(mx)


from math import gcd

def solve(la, ra, ta, lb, rb, tb):
	if la > lb:
		(la, ra, ta, lb, rb, tb) = (lb, rb, tb, la, ra, ta)
	da = ra - la
	db = rb - lb
	ans = 0
	dist = lb - la
	g = gcd(ta, tb)
	dist %= g
	if dist == 0:
		return min(da + 1, db + 1)
	(la, ra) = (lb - dist, lb - dist + da)
	ans = max(ans, 1 + min(rb, ra) - lb)
	ans = max(ans, 1 + min(rb, ra + g) - (la + g))
	return ans

def main():
	(la, ra, ta) = [int(i) for i in input().split()]
	(lb, rb, tb) = [int(i) for i in input().split()]
	print(solve(la, ra, ta, lb, rb, tb))
main()


import sys
import math
inp = iter(map(int, sys.stdin.read().split()))
la = next(inp)
ra = next(inp)
ta = next(inp)
lb = next(inp)
rb = next(inp)
tb = next(inp)
if la > lb:
	(la, lb) = (lb, la)
	(ra, rb) = (rb, ra)
	(ta, tb) = (tb, ta)

def area(l1, r1, l2, r2):
	l = max(l1, l2)
	r = min(r1, r2)
	return r - l + 1
d = lb - la
g = math.gcd(ta, tb)
shift = d // g * g
ans = 0
for d in range(-100, 101):
	ans = max(ans, area(la, ra, lb - shift - g * d, rb - shift - g * d))
print(ans)


from math import gcd
C = lambda x, y: (x + y - 1) // y
(l0, r0, t0) = map(int, input().split())
(l1, r1, t1) = map(int, input().split())
g = gcd(t0, t1)
c = [0] * 4
i = min((r1 - r0) // g, (l1 - l0) // g)
if i >= C(l1 - r0, g):
	c[0] = r0 - l1 + g * i + 1
i = min((r0 - r1) // g, (l0 - l1) // g)
if i >= C(l0 - r1, g):
	c[1] = r1 - l0 + g * i + 1
i = min((r1 - r0) // g, (r1 - l0) // g)
if i >= max(C(l1 - r0, g), C(l1 - l0, g)):
	c[2] = r0 - l0 + 1
i = min((r0 - r1) // g, (r0 - l1) // g)
if i >= max(C(l0 - r1, g), C(l0 - l1, g)):
	c[3] = r1 - l1 + 1
print(max(c))


from math import gcd
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
k = gcd(ta, tb)
d1 = la - lb
d1 %= k
if d1 < 0:
	d1 += k
d2 = lb - la
d2 %= k
if d2 < 0:
	d2 += k
la = ra - la + 1
lb = rb - lb + 1
ans = max(0, min(la - d2, lb), min(la, lb - d1))
print(ans)


import math
(a1, b1, c1) = map(int, input().split(' '))
(a2, b2, c2) = map(int, input().split(' '))
x = math.gcd(c1, c2)
delta1 = a1 - a2
delta1 = delta1 % x
if delta1 < 0:
	delta1 += x
delta2 = a2 - a1
delta2 = delta2 % x
if delta2 < 0:
	delta2 += x
a1 = b1 - a1 + 1
a2 = b2 - a2 + 1
sol = max(min(a1 - delta2, a2), min(a1, a2 - delta1))
print(max(0, sol))


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
import math
(la, ra, ta) = mi()
(lb, rb, tb) = mi()
if la > lb:
	(la, ra, ta, lb, rb, tb) = (lb, rb, tb, la, ra, ta)
lb -= la
ra -= la
rb -= la
dif = math.gcd(ta, tb)
difl = lb
off1 = difl % dif
ans1 = min(ra, off1 + rb - lb) - max(0, off1) + 1
off2 = off1 - dif
ans2 = min(ra, off2 + rb - lb) - max(0, off2) + 1
ans = max(ans1, ans2, 0)
print(ans)


import sys
from math import *
readints = lambda : map(int, input().strip('\n').split())
(la, ra, ta) = readints()
(lb, rb, tb) = readints()
k = (la - lb) / gcd(ta, tb)
klo = floor(k)
khi = ceil(k)

def intersection(ax, ay, bx, by):
	(l, r) = (max(ax, bx), min(ay, by))
	if l > r:
		return 0
	return r - l + 1
ans = 0
for dk in [klo, khi]:
	df = lb - la + dk * gcd(ta, tb)
	bx = la + df
	by = bx + (rb - lb)
	ans = max(ans, intersection(la, ra, bx, by))
print(ans)


from sys import stdin, stdout
from math import gcd

def main():
	(l_a, r_a, t_a) = readline()
	(l_b, r_b, t_b) = readline()
	lucky_days_a = r_a - l_a + 1
	lucky_days_b = r_b - l_b + 1
	gcd_ = gcd(t_a, t_b)
	return min(lucky_days_a, lucky_days_b, max(lucky_days_a - (l_b - l_a) % gcd_, lucky_days_b - (l_a - l_b) % gcd_, 0))

def readline():
	return map(int, stdin.readline().strip().split())
stdout.write(str(main()) + '\n')


import math
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
if rb - lb < ra - la:
	(la, ra, ta, lb, rb, tb) = (lb, rb, tb, la, ra, ta)
t = math.gcd(ta, tb)

def sect(k):
	return max(0, min(rb, ra + k * t) - max(lb, la + k * t) + 1)
ans = max((sect(x) for x in [(rb - ra) // t, (lb - la + t - 1) // t]))
print(ans)


a = input().split()
b = input().split()
la = int(a[0])
ra = int(a[1])
ta = int(a[2])
lb = int(b[0])
rb = int(b[1])
tb = int(b[2])

def gcd(x, y):
	if x * y == 0:
		return x + y
	elif x > y:
		return gcd(x % y, y)
	else:
		return gcd(x, y % x)
g = gcd(ta, tb)
if la % g >= lb % g:
	temp1 = min(rb - (la % g - lb % g) - lb + 1, ra - la + 1)
	temp2 = min(ra - la + 1 - (g - la % g + lb % g), rb - lb + 1)
	best = max(temp1, temp2, 0)
else:
	temp1 = min(ra - (lb % g - la % g) - la + 1, rb - lb + 1)
	temp2 = min(rb - lb + 1 - (g - lb % g + la % g), ra - la + 1)
	best = max(temp1, temp2, 0)
print(best)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
if ta == 1 or tb == 1:
	print(max(ra - la + 1, rb - lb + 1))
elif math.gcd(ta, tb) == 1:
	print(min(ra - la + 1, rb - lb + 1))
else:
	d = math.gcd(ta, tb)
	mindif = (lb - la + d) % d
	dif = rb - lb
	lb = mindif
	rb = lb + dif
	ra = ra - la
	la = 0
	ans = max(0, min(ra, rb) - lb + 1)
	la += d
	ra += d
	ans1 = max(0, min(ra, rb) - la + 1)
	print(max(ans1, ans))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lucky(l_a, r_a, t_a, l_b, r_b, t_b):
	if r_b - l_b > r_a - l_a:
		thing = [l_a, r_a, l_b, r_b]
		l_a = thing[2]
		r_a = thing[3]
		l_b = thing[0]
		r_b = thing[1]
	if l_a == l_b or r_a == r_b:
		return r_b - l_b + 1
	else:
		gcdt = gcd(t_a, t_b)
		M = gcdt - (l_a - l_b) % gcdt
		if M % gcdt == 0:
			M = 0
		overhang = max(M + (r_b - l_b + 1) - (r_a - l_a + 1), 0)
		overlap = max(r_b - l_b + 1 - overhang, 0)
		N = gcdt - (r_b - r_a) % gcdt
		if N % gcdt == 0:
			N = 0
		overhang = max(0, N + (r_b - l_b + 1) - (r_a - l_a + 1))
		overlap = max(overlap, max(0, r_b - l_b + 1 - overhang))
		return overlap
while True:
	A = input().split()
	l_a = int(A[0])
	r_a = int(A[1])
	t_a = int(A[2])
	B = input().split()
	l_b = int(B[0])
	r_b = int(B[1])
	t_b = int(B[2])
	print(lucky(l_a, r_a, t_a, l_b, r_b, t_b))
	break


import math
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
(x, d) = (math.gcd(a[2], b[2]), a[0] - b[0])
print(max(min(a[1] - a[0] + 1, b[1] - b[0] + 1 - d % x), min(b[1] - b[0] + 1, a[1] - a[0] + 1 - -d % x), 0))


def get_cover(l1, r1, l2, r2):
	return max(0, min(r1, r2) - max(l1, l2) + 1)

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(A, B, T) = map(int, input().split())
(C, D, F) = map(int, input().split())
g = gcd(F, T)
diff = A - C
seg1 = diff % g
seg2 = diff % g - g
val1 = get_cover(seg1, seg1 + B - A, 0, D - C)
val2 = get_cover(seg2, seg2 + B - A, 0, D - C)
print(max(val1, val2))


(lA, rA, tA) = [int(x) for x in input().split()]
(lB, rB, tB) = [int(x) for x in input().split()]
rA += 1
rB += 1

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
move = gcd(tA, tB)
m1 = (lB - lA) // move
lA += m1 * move
rA += m1 * move
first = min(rA, rB) - max(lA, lB)
lA += move
rA += move
second = min(rA, rB) - max(lA, lB)
print(max(0, first, second))


(la, ra, ta) = list(map(int, input().split()))
(lb, rb, tb) = list(map(int, input().split()))
(a, b) = (ta, tb)
while b:
	(a, b) = (b, a % b)
ra -= la // a * a
la %= a
rb -= lb // a * a
lb %= a
print(max(min(ra, rb) + 1 - max(la, lb), min(ra + a, rb) + 1 - max(la + a, lb), min(ra, rb + a) + 1 - max(la, lb + a), 0))


(l1, r1, t1) = map(int, input().split())
(l2, r2, t2) = map(int, input().split())

def gcd(a, b):
	if a > b:
		(a, b) = (b, a)
	while a > 0:
		b = b % a
		(a, b) = (b, a)
	return b
d = gcd(t1, t2)

def answer(x1, d1, x2, d2):
	if x1 > x2:
		(x1, x2, d1, d2) = (x2, x1, d2, d1)
	d1 = d1 - (x2 - x1)
	if d1 < 0:
		d1 = 0
	return min(d1, d2)
d1 = r1 - l1 + 1
d2 = r2 - l2 + 1
l1 = l1 % d
l2 = l2 % d
if l1 > l2:
	(l1, l2, d1, d2) = (l2, l1, d2, d1)
ans1 = answer(l1, d1, l2, d2)
ans2 = answer(l1 + d, d1, l2, d2)
print(max(ans1, ans2))


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(la, ra, ta) = getIntList()
(lb, rb, tb) = getIntList()
g = gcd(ta, tb)
wa = ra - la + 1
wb = rb - lb + 1
d = la - lb
d1 = d // g * g
la -= d1
ra -= d1
while la < lb:
	la += g
	ra += g
while la >= lb:
	la -= g
	ra -= g

def work():
	x0 = max(la, lb)
	x1 = min(ra, rb)
	r = x1 - x0 + 1
	return r
res = 0
res = max(work(), res)
while la < lb:
	la += g
	ra += g
res = max(work(), res)
print(res)


import math
(a, b, c) = map(int, input().split())
(d, e, f) = map(int, input().split())
g = math.gcd(c, f)
int1 = [a % g, b % g]
in1 = b // g - a // g
int2 = [d % g, e % g]
in2 = e // g - d // g
int1[1] += g * in1
int2[1] += g * in2
maybegood = max(min(int1[1], int2[1]) - max(int1[0], int2[0]) + 1, 0)
int2[0] += g
int2[1] += g
maybegood1 = max(min(int1[1], int2[1]) - max(int1[0], int2[0]) + 1, 0)
int2[0] -= 2 * g
int2[1] -= 2 * g
maybegood2 = max(min(int1[1], int2[1]) - max(int1[0], int2[0]) + 1, 0)
print(max(maybegood, maybegood1, maybegood2))


import math

def gcd(a, b):
	if b > a:
		return gcd(b, a)
	if a % b == 0:
		return b
	return gcd(b, a % b)
(la, ra, ta) = [int(s) for s in input().split(' ')]
(lb, rb, tb) = [int(s) for s in input().split(' ')]
delta = gcd(ta, tb)

def overlap(al, ar, bl, br):
	return max(0, min(ar, br) - max(al, bl) + 1)

def eval(fst_, snd_):
	result_ = 0
	k1 = math.ceil(abs(fst_[0] - snd_[0]) / delta)
	k2 = abs(fst_[0] - snd_[0]) // delta
	for k in (k1, k2):
		pos_sndl = snd_[0]
		pos_sndr = snd_[1]
		pos_fstl = k * delta + fst_[0]
		pos_fstr = k * delta + fst_[1]
		intersection = overlap(pos_sndl, pos_sndr, pos_fstl, pos_fstr)
		result_ = max(result_, intersection)
	return result_
fst_ = (la, ra, ta)
snd_ = (lb, rb, tb)
result = max(eval(fst_, snd_), eval(snd_, fst_))
print(result)


from sys import stdin, stdout
from math import sin, tan, cos, pi, atan2, sqrt, acos, atan, factorial

def gcd(a, b):
	if not b:
		return a
	else:
		return gcd(b, a % b)

def solution(la, ra, ta, lb, rb, tb):
	g = gcd(ta, tb)
	d = (lb - la) % g
	return min(ra - la + 1 - d, rb - lb + 1)
(la, ra, ta) = map(int, stdin.readline().split())
(lb, rb, tb) = map(int, stdin.readline().split())
ans = max(solution(la, ra, ta, lb, rb, tb), solution(lb, rb, tb, la, ra, ta))
stdout.write(str(max(0, ans)))


(la, ra, ta) = map(int, input().split())
(lb, rb, tb) = map(int, input().split())
g = 0

def gcd(a, b):
	global g
	if b == 0:
		g = a
		return a
	else:
		gcd(b, a % b)
gcd(ta, tb)
difff = ((lb - la) % g + g) % g
da = ra - la + 1
db = rb - lb + 1
ans = max(min(max(da - difff, 0), db), min(max(db - (g - difff), 0), da))
print(ans)


def gcd(a, b):
	return b if a == 0 else gcd(b % a, a)
(l1, r1, t1) = map(int, input().split())
(l2, r2, t2) = map(int, input().split())
g = gcd(t1, t2)
d = l2 - l1
s = d % g
ans1 = min(r1 - l1 - s, r2 - l2) + 1
s = (g - s) % g
ans2 = min(r1 - l1, r2 - l2 - s) + 1
print(max(ans1, ans2, 0))