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.

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.

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

NoteThe 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.

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld  long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define pii pair<int,int>
#define mod2 998244353LL
#define setbit(x) __builtin_popcount(x)
typedef tree< pair<int, int>,null_type,less<pair<int, int> >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

ll a[2][3];
ll ans=0;

void fun(int i1, int i2){
map<ll,int> m1;

ll s1 = a[i1][0];
ll s2 = a[i2][0];

if(s1>s2)
{
    int x = (s1-s2)/a[i2][2];
    s2+=a[i2][2]*(x+1);
}
ll diff = s2-s1;
ll gc = __gcd(a[i1][2],a[i2][2]);

    ll vl = diff/gc;
    diff -= gc*vl;

/*int cnt=100000;
while(cnt--)
{
     int x = (s2-s1)/a[i1][2];
     s1+=a[i1][2]*(x);
     diff = min(diff, s2-s1);
     s1+=a[i1][2];

     x = (s1-s2)/a[i2][2];
    s2+=a[i2][2]*(x+1);

}
diff = max(0LL,diff);
*/
ans = max(ans, min(a[i1][1]-a[i1][0]+1LL - diff  , a[i2][1]-a[i2][0]+1LL));

}

void solve()
{
for(int i=0;i<2;i++)
    cin>>a[i][0]>>a[i][1]>>a[i][2];

fun(0,1);
fun(1,0);
cout<<ans;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt","r",stdin);
   // freopen("output.txt","w",stdout);
    int t=1 ;
    //cin>>t;

    while(t--)
     {
      solve();
     }
    return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long double LD;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef LOCAL
#define DEBUG(x) { cerr << "# " << #x << ": " << x << endl;}
#else
#define DEBUG(x)
#endif
mt19937_64 mt(time(0));
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/
const LL MAXN = 2e9;
LL gcd(LL a, LL b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

LL solve(LL a, LL b, LL x, LL y) {
    if (b < x || y < a) return 0;
    return min(b, y) - max(a, x) + 1;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cout.precision(10); cout << fixed;
#ifdef LOCAL
    freopen("../test.txt", "r", stdin);
    // freopen("../output.txt", "w", stdout);
#endif
    LL a, b, c, x, y, z;
    cin >> a >> b >> c >> x >> y >> z;
//    a += MAXN * c;
//    b += MAXN * c;
//    x += MAXN * z;
//    y += MAXN * z;
    if (b-a+1 == c && y-x+1 == z) assert(false);
    if (b-a+1 == c) return y-x+1;
    if (y-x+1 == z) return b-a+1;
    LL tmp = gcd(c, z), delta, ret = 0;
    delta = ((a - x) % tmp + tmp) % tmp;
    ret = solve(0, y-x, delta, delta+b-a);
    ret = max(ret, solve(0, y-x, delta - tmp, delta - tmp+b-a));
    cout << ret << endl;

/*---------------------------------------Coding Space End------------------------------------------*/
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}
/* author:  txingml */
/* picture is from hamayanhamayan(CF handle) */

#include<bits/stdc++.h>
#define int long long
using namespace std;
// PEACE
int l1, r1, t1, l2, r2, t2;

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> l1 >> r1 >> t1 >> l2 >> r2 >> t2;
	int ll = l1;
	int rr = r1;
	int tt = t1;
	int lll = l2;
	int rrr = r2;
	int ttt = t2;
	int l = r1 - l1 + 1;
	int g = gcd(t1, t2);
	l1 = l1 % g;
	int x = (l2 / g) * g;
	x += l1;
	while(x < l2)
		x = x + g;
	int res1 = r2 - x + 1;
	res1 = min(res1, l);

	l1 = ll;
	l2 = lll;
	r1 = rr;
	r2 = rrr;
	t1 = tt;
	t2 = ttt;

	int L = r2 - l2 + 1;
	int G = gcd(t1, t2);
	l2 = l2 % G;
	int X = (l1 / G) * G;
	X += l2;
	while(X < l1)
		X = X + G;
	int res2 = r1 - X + 1;
	res2 = min(res2, L);
	int ans = max(res1, res2);
	ans = max(ans, 0LL);
	return cout << ans, 0;
}

#include <bits/stdc++.h>
#define LL long long
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;

const int N = 1e5;

LL la,ra,ta;
LL lb,rb,tb;

int main(){
    //freopen("D:\\rush.txt","r",stdin);
    scanf("%lld%lld%lld",&la,&ra,&ta);
    scanf("%lld%lld%lld",&lb,&rb,&tb);
    if (la>lb){
        swap(la,lb);
        swap(ra,rb);
        swap(ta,tb);
    }
    //la <= lb
    /*
        st1 = la + k1*t1
        st2 = lb + k2*t2
        dif = st2-st1
            = lb-la+k2*t2-k1*t1 = 0

        lb-la+k2*t2=k1*t1
        t1*k1=k2*t2+lb-la

        dir = lb-la+k*gcd(t1,t2)
        la-lb=k*gcd(t1,t2);
        k = (la-lb)/gcd(t1,t2);
    */
    LL temp1 = la-lb;
    LL temp2 = __gcd(ta,tb);
    if (temp1%temp2==0){
        LL ans = min(rb-lb+1,ra-la+1);
        printf("%lld\n",ans);
    }else{
        double temp = 1.0*temp1/temp2;

        LL len1 = ra-la+1,len2 = rb-lb+1;
        LL k = (LL) (ceil(temp));

        LL delta = lb-la+k*__gcd(ta,tb);

        LL aa = 1,bb = len1,cc = 1+delta,dd = delta+len2;
        LL ans = 0;
        ans = max(ans,min(bb,dd)-max(aa,cc)+1);

        k = (LL) (floor(temp));
        delta = lb-la+k*__gcd(ta,tb);
        aa = 1,bb = len1,cc = 1+delta,dd = delta+len2;
        ans = max(ans,min(bb,dd)-max(aa,cc)+1);

        printf("%lld\n",ans);
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair

ll NearestOffset(ll x, ll g){
  return (g + (x % g)) % g;
}

ll Solve(ll la, ll ra, ll ta, ll lb, ll rb, ll tb){
  ll g = __gcd(ta, tb);
  ll x = NearestOffset(lb - la, g);
  return max(0ll, min(x + rb - lb, ra - la) - max(0ll, x) + 1);
}

int main(){
  ll la, ra, ta, lb, rb, tb;

  cin >> la >> ra >> ta >> lb >> rb >> tb;

  cout << max(Solve(la, ra, ta, lb, rb, tb), Solve(lb, rb, tb, la, ra, ta)) << '\n';

  return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long double LD;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef LOCAL
#define DEBUG(x) { cerr << "# " << #x << ": " << x << endl;}
#else
#define DEBUG(x)
#endif
mt19937_64 mt(time(0));
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/
const LL MAXN = 2e9;
LL gcd(LL a, LL b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

LL solve(LL a, LL b, LL x, LL y) {
    if (b < x || y < a) return 0;
    return min(b, y) - max(a, x) + 1;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cout.precision(10); cout << fixed;
#ifdef LOCAL
    freopen("../test.txt", "r", stdin);
    // freopen("../output.txt", "w", stdout);
#endif
    LL a, b, c, x, y, z;
    cin >> a >> b >> c >> x >> y >> z;
    a += MAXN * c;
    b += MAXN * c;
    x += MAXN * z;
    y += MAXN * z;
    if (b-a+1 == c && y-x+1 == z) assert(false);
    if (b-a+1 == c) return y-x+1;
    if (y-x+1 == z) return b-a+1;
    LL tmp = gcd(c, z), delta, ret = 0;
    delta = ((a - x) % tmp + tmp) % tmp;
    ret = solve(0, y-x, delta, delta+b-a);
    ret = max(ret, solve(0, y-x, delta - tmp, delta - tmp+b-a));
    cout << ret << endl;

/*---------------------------------------Coding Space End------------------------------------------*/
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}
/* author:  txingml */
/* picture is from hamayanhamayan(CF handle) */

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<ll, ll> Pair;

ll gcd(ll a, ll b) {
    while(b != 0) {
        a = a%b;
        swap(a, b);
    }
    return a;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll la, ra, ta;
    ll lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    if(ra - la < rb - lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }
    int ka = ra - la;
    int kb = rb - lb;
    ll g = gcd(ta, tb);
    la %= g;
    ra = la + ka;
    lb%=g;
    rb = lb + kb;
    ll t1 = min(ra, rb) - max(la, lb) + 1;
    ll t2 = min(ra + g, rb) - max(la+g, rb) + 1;
    ll t3 = min(ra, rb+g) - max(la, lb+g) + 1;
    cout << max(max(t1, t2), max(t3, (ll)0)) << endl;

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll la,ra,ta,lb,rb,tb;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>la>>ra>>ta>>lb>>rb>>tb;

	if(la>lb)
	{
		swap(la,lb);
		swap(ra,rb);
		swap(ta,tb);
	}
	ll tmp=__gcd(ta,tb);

	/*if((lb-la)%tmp==0)
	{
		cout<<max(-lb+ra+1,0ll)<<endl;
	}*/
	/*else
	{*/

	ll ans=0,move=((lb-la)/tmp)*tmp;
	la += move;
	ra+=move;
	// (ra-la+1)-(lb-la) ,  (
	ans=min(rb-lb+1,ra-lb+1);
	ans=max(0ll,ans);
	la+=tmp;
	ra+=tmp;
	ll ans1=min(rb-la+1,ra-la+1);
	ans=max(ans,ans1);
	cout<<ans<<endl;
	/*}*/
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
struct node{
	ll L,R,T;
	ll len(){return R-L+1;}
	void in(){scanf("%I64d%I64d%I64d",&L,&R,&T);}
	node(){}
	node(ll a,ll b,ll c):L(a),R(b),T(c){}
	void operator +=(ll a){L+=a;R+=a;}
}A,B;
ll ans,D;
ll floordiv(ll a,ll b){return (a/b)*b;}
ll ceildiv(ll a,ll b){return (a/b+1)*b;}
void calc(node a,node b){
	ll res=0;
	if(a.L<=b.L){
		res=(b.L-a.L)-floordiv((b.L-a.L),D);
	}else{
		res=ceildiv(a.L-b.L-1,D)-(a.L-b.L);
	}
	if(res<=a.len())ans=max(ans,min(a.len()-res,b.len()));
}
int main(){
	A.in();B.in();
	D=gcd(A.T,B.T);
	calc(A,B);
	calc(B,A);
	printf("%I64d",ans);
	return 0;
}

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pb push_back
#define N 100003
#define inf (ll)1e9+11
#define warmup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int mod=1e9+7;

signed main()
{
    warmup

    int la,lb,ra,rb,ta,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;

    int g=__gcd(ta,tb);
    int k1=(la>lb ? la-lb+g-1:la-lb)/g, k2=(la<lb ? la-lb-g+1:la-lb)/g;
    int d1=lb-la + k1*g, d2=lb-la + k2*g;
    int ans=0;

    int ln=la+d1, rn=ln+rb-lb;
    ans=max(ans,min(ra,rn)-max(la,ln)+1);

    ln=la+d2, rn=ln+rb-lb;
    ans=max(ans,min(ra,rn)-max(la,ln)+1);

    cout<<ans;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 2e5 + 5;

LL Ex_line(LL l,LL r,LL L,LL R) {
	LL t1 = max(l,L);
	LL t2 = min(r,R);
	return max(t2 - t1 + 1,0ll);
}
int main() {
	LL l,r,t;
	LL L,R,T;
	scanf("%lld %lld %lld",&l,&r,&t);
	scanf("%lld %lld %lld",&L,&R,&T);
	LL d = __gcd(t,T);
	LL len1 = r - l;
	LL len2 = R - L;
	l %= d,L %= d;
	r = l + len1;
	R = L + len2;
//	printf("%lld %lld %lld %lld\n",l,r,L,R);
	LL ans = Ex_line(l,r,L,R);
	ans = max(ans,Ex_line(l + d, r + d, L , R));
	ans = max(ans,Ex_line(l , r, L + d, R + d));
	cout << ans;
}

                                                                                /*
                  ooo        ooooo                  .o88o.     o8o
                  `88.       .888'                  888 `"     `"'
                   888b     d'888      .oooo.      o888oo     oooo      .oooo.
                   8 Y88. .P  888     `P  )88b      888       `888     `P  )88b
                   8  `888'   888      .oP"888      888        888      .oP"888
                   8    Y     888     d8(  888      888        888     d8(  888
                  o8o        o888o    `Y888""8o    o888o      o888o    `Y888""8o

                                     __         __  ___
                                    (_  _ |_   /  \   /  |\ | _    _ _ |_  _ _
                                    __)(_||_   \__/  /   | \|(_)\/(-||||_)(-|

                                                                                */
#include <bits/stdc++.h>
using namespace std;

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define pb                                        push_back
#define all(v)                                    v.begin(),v.end()
#define endl						  '\n'

const int N = 2e6 + 7, M = (int)1e9 + 7, inf = INT_MAX; const long long INF = LLONG_MAX;

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
#define rtn                                       if(1)return;
template <typename Arg1> void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) { rtn const char* comma = strchr (names + 1, ','); cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...); }
int la, ra, ta, lb, rb, tb, period1, period2, p2, p1;

int get(int l){
      l %= period1;
      if (l < 0 ) l += period1;
      bug(l);
      if( p2 - 1 >= period1 ){
            int a = max(0ll, ra  - l + 1);
            int left = p2 - (period1 - l);
            int b = min(ra + 1, left);
            return max(a, b);
      }

      int r = (l + p2 - 1)%period1;

      if( l <= r ) return max(0ll, min(r, ra) - l + 1);
      else{
            int a = max(0ll, ra - l + 1);
            int b = min(r, ra) + 1;
            return max(a, b);
      }
}

void solve()
{
      cin >> la >> ra >> ta >> lb >> rb >> tb;
      if( la > lb ) swap(la, lb), swap(ra,rb), swap(ta, tb);
      int shift = la;
      la -= shift, ra -= shift;
      lb -= shift, rb -= shift;

      period1 = ta, period2 = tb;

      int x = __gcd( period1, period2 );
      int start = lb%period1;

      p2 = rb - lb + 1;
      bug(start);
      bug(la, ra, ta);
      bug(lb, rb, tb);
      bug(period1, period2);
      p1 = ra - la + 1;
      bug(p2, x, p1);

                  // start + k*x >= period1

      int ans = 0;
      ans = max(ans, get(start) );
      int k1 = (period1 - start + x - 1 )/x;
      for(int i = -10;  i <= 10 ; i++){
            ans = max(ans, get(start + (k1+i)*x) );
      }

      cout << ans << endl;
}

int32_t main()
{
      ios_base::sync_with_stdio(0);   cin.tie(0);   cout.tie(0);

      int test = 1;
      // cin >> test;
      while(test--)solve();

}

#include <algorithm>
#include <iostream>
#include <memory.h>
#include <iterator>
#include <cassert>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define forn(i, n) for (int i = 0; i < int(n); ++i)

#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()

const int N = (int) 1e6 + 10;
const int MOD = (int) 1e9 + 7;
const int MMOD = (int) 1e7 + 7;

ll bin_pow (ll a , ll n) {
    if(n == 0){
        return 1;
    }
    if(n % 2 == 1){
        ll cnt = (a * bin_pow(a , n - 1));
        return cnt;
    }
    else {
        ll cnt = bin_pow(a , n / 2);
        return (cnt * cnt);
    }
}
ll gcd(ll a, ll b) {
    return b ? gcd(b, a%b) : a;

}

int main(){
    ios_base::sync_with_stdio(0);
    //freopen("input.txt" , "r" , stdin);
    //freopen("output.txt" , "w" , stdout);

    ll l1,l2,r1,r2,t1,t2;
    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;

    ll kek = gcd(t1,t2);

    ll la = ((l1 - l2) % kek + kek) % kek;
    ll lb = ((l2 - l1) % kek + kek) % kek;

    ll ma = min(r1 - l1,r2 - l2 - la) + 1;
    ll ba = min(r1 - l1 - lb, r2 - l2) + 1;

    ll ans = max(ma, ba);

    cout << max(ans, (ll)0);

    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

ll calc(ll shft, ll la, ll ra, ll lb, ll rb){
    la += shft, ra += shft;
    ll lll = max(la,lb);
    ll rrr = min(ra,rb);
    return rrr-lll+1;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    ll la,ra,ta,lb,rb,tb;
    sc(la,ra,ta,lb,rb,tb);
    ll gc = __gcd(ta,tb);
    if(gc == 1){
        REE(min(ra-la+1,rb-lb+1))
    }
    ll ans = 0;
    vector<ll> shf = {0};
    ll xd = (lb-la);
    /* whatis(xd) */
    /* ll xdd = xd-(xd%gc); */
    ll xdd = xd/gc*gc;
    for(int i = -1; i <= 1; ++i)
        shf.pb(xdd+gc*i);
    xd = (la-ra);
    /* xdd = xd-(xd%gc); */
    xdd = xd/gc*gc;
    for(int i = -1; i <= 1; ++i)
        shf.pb(xdd+gc*i);
    /* whatis(shf) */
    FORR(i,shf){
        ans = max(ans,calc(i,la,ra,lb,rb));
        ans = max(ans,calc(i,lb,rb,la,ra));
    }
    cout << ans << '\n';
}

//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

const long long inf = 1000*1000*1000+7;
const long long INF = inf*inf;
const long long MOD = inf;
const long long N = 1e5+10, base=1<<18;

void solve(){
    int la, ra, ta;
    int lb, rb, tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;
    if(__gcd(ta,tb)==1)
    {
        cout<<min(ra-la+1,rb-lb+1)<<"\n";
        return;
    }
    int gcd=__gcd(ta,tb);
    int ile=la/gcd;
    ra-=ile*gcd;
    la%=gcd;
    debug(ile);
    ile=lb/gcd;
    lb%=gcd;
    rb-=ile*gcd;
    debug(la,ra,ta);
    debug(lb,rb,tb);
    int res=0;
    res=max(res,min(ra,rb)-max(la,lb)+1);
    res=max(res,min(ra,rb-gcd)-max(la,lb-gcd)+1);
    res=max(res,min(ra-gcd,rb)-max(la-gcd,lb)+1);
    cout<<res<<"\n";
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);
    cout<<setprecision(9)<<fixed;
    cerr<<setprecision(6)<<fixed;

    int test = 1;
//    cin>>test;
    while(test--){
    	solve();
    }
}
/*
1 1 2
1 4 5
*/

/*#include<bits/stdc++.h>
using namespace std;*/
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<map>
int m,a,b,c,r,l,x,y,z,i,g,p,q,o;
int main()
{
    scanf("%d%d%d",&a,&b,&c);
    scanf("%d%d%d",&x,&y,&z);
    g=std::__gcd(c,z);
    p=b-a+1;
    q=y-x+1;
    r=x-a;
    o=r%g;
    if(o<0)o+=g;

    m=std::min(std::max(p-o,0),q);

    m=std::max(m,std::min(p,std::max(q-(g-o),0)));
    printf("%d",m);

}

#include <bits/stdc++.h>
#define base 1000000007LL
#define ll long long
#define X first
#define Y second
#define ld double
#define pb push_back
#define EPS 1e-9
#define all(a) a.begin(),a.end()
#define sz(a) int((a).size())
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define what_is(x) cout << #x << " is " << x << endl;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
typedef pair<int, int> II;
typedef pair<ll, II> III;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}

ll lA, rA, tA, lB, rB, tB;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a%b);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> lA >> rA >> tA;
    cin >> lB >> rB >> tB;
    if (rA == tA-1 && lA == 0) {
        cout << rB - lB + 1;
        return 0;
    }
    if (rB == tB-1 && lB == 0) {
        cout << rA - lA + 1;
        return 0;
    }
    if (tA > tB) {
        swap(lA, lB);
        swap(rA, rB);
        swap(tA, tB);
    }
    ll lenA = rA - lA + 1;
    ll lenB = rB - lB + 1;
    ll g = gcd(tA, tB);

    int kcA = lA + tA - rA - 1;
    int kcB = lB + tB - rB - 1;

    if (lB < lA) {
        lA -= (lA - lB) / g * g;
    }
    else if (lB > lA) {
        lA += ((lB - lA) / g + ((lB - lA) % g > 0)) * g;
    }
    // else {
    //     cout << tB / tA * (rA - lA + 1) + min(tB % tA, rA - lA + 1);
    // }
    ll dist = lA - lB;
    ll realDist = max(0LL, lenB - dist);
    ll res = min(realDist, lenA);

    lA -= g;
    dist = lB - lA;
    realDist = max(0LL, lenA - dist);
    res = max(res, min(realDist, lenB));

    cout << res;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>>(h, vector<type>(w)) v; rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); solve(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

ll intersect(ll la, ll ra, ll lb, ll rb) {
  if (la > lb) {
    swap(la, lb);
    swap(ra, rb);
  }
  return max(0LL, min(ra, rb) - lb + 1);
}

ll solve(ll la, ll ra, ll ta, ll lb, ll rb, ll tb) {
  ll g = __gcd(ta, tb);
  ll inf = 1e9;
  lb += g * inf, rb += g * inf;
  ll diff = lb - la;
  la += (diff / g) * g;
  ra += (diff / g) * g;
  ll ans = 0;
  rep(i, 0, 2) {
    // dbg(la, ra, lb, rb);
    chmax(ans, intersect(la, ra, lb, rb));
    la += g, ra += g;
  }
  // dbg(la, ra, lb, rb, g);
  return ans;
}

void solve() {
  rd(ll, la, ra, ta);
  rd(ll, lb, rb, tb);
  wr(max(solve(lb, rb, tb, la, ra, ta), -1LL));
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;

using pii = pair<int,int>;
using vi = vector<int>;
const int maxn = 1e5+6;
ll a[2], b[2], c[2];

inline ll getres(ll a, ll b, ll c, ll d) {
    return max(min(b, d) - max(a, c) + 1, 0ll);
}

int main(int argc,char *argv[])
{
    scanf("%lld%lld%lld%lld%lld%lld", &a[0], &b[0], &c[0],
          &a[1], &b[1], &c[1]);
    if (c[1] < c[0]) {
        swap(c[0], c[1]);
        swap(a[0], a[1]);
        swap(b[0], b[1]);
    }
    ll t = a[1] - a[0];
    ll mink = __gcd(c[0], c[1]);
//    cout << a[0] % mink - a[1] % mink << endl;
    b[0] -= a[0];
    b[1] -= a[1];
    t %= mink;
    ll realres = 0;
//    cout << t << " " << mink << endl;
    for (int i = -42; i < 42; ++i) {
        ll x = mink * i + t;
//        cout << "*" << i * mink << " " << getres(0, b[0], x, b[1] + x) << endl;
        realres = max(realres, getres(0, b[0], x, b[1] + x));
    }
//    cout << b[0] << " " << b[1] << " " << t << " " << mink << endl;
//    do {
//        realres = resnow;
//        resnow = getres(b[0], b[1] + t, t);
////        cout << "*" << t << " " << resnow << endl;
//        t += mink;
//    } while (t <= len);
//    realres = max(realres, resnow);
    printf("%lld\n", realres);
    return 0;
}

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>

using namespace std;

typedef long long ll;

const int lim = 1e8;

int my_solve(int la, int ra, int ta, int lb, int rb, int tb) {
    int dt = abs(ta - tb);
    if (dt == 0) {
        return max(0, min(ra, rb) - max(la, lb) + 1);
    }
    int ans = 0;
    int k = (rb - ra) / gcd(dt, tb);
    int rr = (ra + k * gcd(dt, tb)) % tb;
    int ll = -ra + la + rr;
    ans = min(rb, rr) - max(lb, ll) + 1;
    k++;
    rr = (ra + k * gcd(dt, tb)) % tb;
    ll = -ra + la + rr;
    ans = max(ans,  min(rb, rr) - max(lb, ll) + 1);
    return max(ans, 0);
}

int ok_solve(int la, int ra, int ta, int lb, int rb, int tb) {
    if (gcd(ta, tb) == 1) {
        return min(rb - lb + 1, ra - la + 1);
    } else {
        int ans = 0;
        if (ra > rb) {
            swap(ra, rb);
            swap(la, lb);
            swap(ta, tb);
        }
        int dt = abs(ta - tb);
        for (int i = 0; i < lim; i++) {
            la += dt;
            ra += dt;
            if (ra > la) {
                int ll = max(la, lb);
                int rr = min(ra, rb);
                ans = max(rr - ll + 1, ans);
            }
        }
        return ans;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef LOCAL
    assert(freopen("input.txt", "r", stdin));
    assert(freopen("output.txt", "w", stdout));
#endif

    int la, lb, ra, rb, ta, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    int f = my_solve(la, ra, ta, lb, rb, tb);
    int s = my_solve(lb, rb, tb, la, ra, ta);
    cout << max(f, s);
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long la, ra, ta;
    long long lb, rb, tb;

    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;

    if(ra - la < rb - lb)
    {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }

    long long k = __gcd(ta, tb);

    ra -= la;
    lb -= la;
    rb -= la;
    la = 0;

    long long pos = lb % tb;
    if(pos < 0)
        pos += tb;

   // cout << pos << endl;

    pos %= k;

   // cout << pos << ", " << k << endl;

    long long sol1 = min(rb - lb + 1, ra - pos + 1);
    long long sol2 = max(0LL, pos - k + (rb - lb) - la + 1);

   // cout << sol1 << " " << sol2 << endl;

    long long sol = max(sol1, sol2);

    cout << sol << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const int INF = 1e9 + 5;

typedef long long ll;

ll la, ra, ta, lb, rb, tb;
ll ans;

int main() {
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    ll t = __gcd(ta, tb);
    ll l1 = la % t,
    l2 = lb % t;
    ll r1 = l1 + (ra - la),
    r2 = l2 + (rb - lb);
    for (ll i = -2; i <= 2; i++) {
        for (ll j = -2; j <= 2; j++) {
            la = l1 + ta * i;
            ra = r1 + ta * i;
            lb = l2 + tb * j;
            rb = r2 + tb * j;
            ans = max(ans, min(ra, rb) - max(la, lb) + 1);
        }
    }
    cout << ans;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a,ll b){
    return b == 0 ? a : gcd(b,a%b);
}

int main(){
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;
    ll Lena = ra - la + 1;
    ll Lenb = rb - lb + 1;
    ll g = gcd(ta,tb);
    ll _g = g;
    // cout<<"g = "<<g<<endl;
    ll ans = 0;
    if(la == lb){
        ans = max(ans,min(Lena,Lenb));
    }
    if(la < lb){
        ll sub = (lb - la) % _g;
        ans = max(ans,min(Lena - sub,Lenb));
        sub = _g - sub;
        ans = max(ans,min(Lena,Lenb - sub));
    }
    if(la > lb){
        ll sub = (la - lb) % _g;
        ans = max(ans,min(Lena,Lenb - sub));
        sub = _g - sub;
        ans = max(ans,min(Lena - sub,Lenb));
    }
    cout<<ans<<endl;
    return 0;
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

#define FOR(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int la, ra, ta, lb, rb, tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int t = __gcd(ta, tb);
    int k1;
    if ((lb - la) % t == 0)
        k1 = (lb - la) / t;
    else{
        if (lb - la >= 0)
            k1 = (lb - la) / t;
        else
            k1 = (lb - la) / t - 1;
    }
    int k2 = k1 + 1;
    long long aaa = min(ra + (long long)k1 * t, (long long)rb) - max((long long)lb, la + (long long)k1 * t),
              bbb = min(ra + (long long)k2 * t, (long long)rb) - max((long long)lb, la + (long long)k2 * t);
    cout << max(max(aaa, bbb) + 1, (long long)0) << endl;
}

# include <bits/stdc++.h>
using namespace std;

int la;
int lb;
int ra;
int rb;
int ta;
int tb;

int main(){
    scanf("%d%d%d%d%d%d",&la,&ra,&ta,&lb,&rb,&tb);
    int t=__gcd(ta,tb);
    int res=0;
    int da=ra-la;
    int db=rb-lb;
    if(la>lb){
        int d=la-lb;
        int p=d/t;
        int L=max(la-p*t,lb);
        int R=min(la-p*t+da,lb+db);
        res=max(res,R-L+1);
        L=max(la-(p+1)*t,lb);
        R=min(la-(p+1)*t+da,lb+db);
        res=max(res,R-L+1);
    }
    else {
        int d=lb-la;
        int p=d/t;
        int L=max(lb-p*t,la);
        int R=min(lb-p*t+db,la+da);
        res=max(res,R-L+1);
        L=max(lb-(p+1)*t,la);
        R=min(lb-(p+1)*t+db,la+da);
        res=max(res,R-L+1);
    }
    printf("%d\n",res);
    return 0;
}
//ORZ

#include <bits/stdc++.h>

using namespace std;

long long lh[2],rh[2],d[2];

int main()
{
	long long it,gd,df,mn;

	for(it=0;it<2;it++)
	{
		scanf("%lld%lld%lld",lh+it,rh+it,d+it);
	}
	if(lh[0]>lh[1])
	{
		swap(lh[0],lh[1]);
		swap(rh[0],rh[1]);
		swap(d[0],d[1]);
	}
	gd=__gcd(d[0],d[1]);
	df=lh[1]-lh[0];
	mn=df/gd;
	printf("%lld\n",max((long long)0,max(min(rh[0]+mn*gd,rh[1])-max(lh[0]+mn*gd,lh[1])+1,min(rh[0]+(mn+1)*gd,rh[1])-max(lh[0]+(mn+1)*gd,lh[1])+1)));
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a1,b1,t1,a2,b2,t2;
    cin>>a1>>b1>>t1>>a2>>b2>>t2;
    long long skok = __gcd(t1,t2);
    long long min1;
    long long min2;
    long long d1,d2,d;
    if(a2>a1)
    {
        d1=(a2-a1)%skok;
        d2=abs(d1-skok);
    }
    else
    {
        d2=(a1-a2)%skok;
        d1=abs(d2-skok);
    }
    //cout<<d1<<" "<<d2<<endl;
        min1 = max(0LL,min(b1-a1+1-d1,b2-a2+1));

        min2 = max(0LL,min(b2-a2+1-d2,b1-a1+1));
    cout<<max(min1,min2);
}

#include<bits/stdc++.h>
using namespace std;
#define INFll 0x3f3f3f3f3f3f3f3f
#define GCD(a,b) __gcd(a,b)
#define ll long long

struct Date
{
    ll l,r,t;
    ll len(){return r-l+1;};
}a,b;

ll F(ll L1,ll R1,ll L2,ll R2)
{
    return max(min(R1,R2)-max(L1,L2)+1,1ll*0);
}
ll Solve()
{
    ll gcd=GCD(a.t,b.t);
    ll c=abs(a.l-b.l);
    if(c%gcd == 0)
        return min(a.len(),b.len());
    ll d1=((b.l-a.l)%gcd+gcd)%gcd;
    ll d2=gcd-d1;
    ll ans=max(F(0,a.r-a.l,d1,d1+b.r-b.l),F(0,b.r-b.l,d2,d2+a.r-a.l));
    return ans;
}
int main()
{
//    freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
    scanf("%lld%lld%lld",&a.l,&a.r,&a.t);
    scanf("%lld%lld%lld",&b.l,&b.r,&b.t);
//    printf("%lld,%lld,%lld\n",a.l,a.r,a.t);
//    printf("%lld,%lld,%lld\n",b.l,b.r,b.t);
    printf("%lld\n",Solve());

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
int fun(int la,int lb,int ra,int rb){
    int MINL = min(la,lb);
    int MAXL = max(la,lb);
    int MINR = min(ra,rb);
    int MAXR = max(ra,rb);
    int ans = MINR - MAXL + 1;
    return ans;
}
int32_t main(){
    int la,ra,ta;
    cin>>la>>ra>>ta;
    int lb,rb,tb;
    cin>>lb>>rb>>tb;
    int r1 = ra - la;
    int r2 = rb - lb;
    int GCD = __gcd(ta,tb);
    la = la%GCD;
    ra = la + r1;
    lb = lb%GCD;
    rb = lb + r2;
    //cout<<la<<" "<<ra<<" "<<lb<<" "<<r
    int ans1 = fun(la,lb,ra,rb);
    int ans2 = fun(la+GCD,lb,ra+GCD,rb);
    int ans3 = fun(la-GCD,lb,ra-GCD,rb);
    int ans4 = fun(la,lb+GCD,ra,rb+GCD);
    int ans5 = fun(la,lb-GCD,ra,rb-GCD);
    int ans = max({(int)0,ans1,ans2,ans3,ans4,ans5});
    cout<<ans;
    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int la,ra,ta,lb,rb,tb,g,k,lap,lbp,start,mx;
int gcd(int a,int b)
{
	if(a<b)
		return gcd(b,a);
	if(b==0)
		return a;
	return gcd(b,a%b);
}
int main()
{
	scanf("%d%d%d%d%d%d",&la,&ra,&ta,&lb,&rb,&tb);
	//a,b
	g=gcd(ta,tb);
	if(la>lb)
	{
		k=(la-lb+tb-1)/tb;
		lbp=lb+k*tb;
	}
	else
		lbp=lb;
	start=(lbp-la)%g+la;
	mx=max(mx,min(ra,start+rb-lb)-start+1);
	//b,a
	if(la<lb)
	{
		k=(lb-la+ta-1)/ta;
		lap=la+k*ta;
	}
	else
		lap=la;
	start=(lap-lb)%g+lb;
	mx=max(mx,min(rb,start+ra-la)-start+1);
	printf("%d\n",mx);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

ll l1, r1, t1, l2, r2, t2;

ll intersct(ll x1, ll y1, ll x2, ll y2){
    ll x3 = max(x1, x2);
    ll y3 = min(y1, y2);
    return max(0ll, y3-x3+1);
}

ll f1(ll l1, ll r1, ll l2, ll r2, ll t){
    ll l = -2e9, r = 2e9;
    while(l < r){
        ll mid = (l+r+1)>>1;
        if (l2 + t*mid <= l1)
            l = mid; else
            r = mid-1;
    }
    ll ans = 0;
    for (ll i = l-3; i <= l+3; i++){
        ll curL = l2 + t*i, curR = r2 + t*i;
        ans = max(ans, intersct(curL, curR, l1, r1));
    }

    return ans;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;
    ll gc = __gcd(t1, t2);
    ll ans = max(f1(l1, r1, l2, r2, gc), f1(l2, r2, l1, r1, gc));

    cout << ans;

}

#include <bits/stdc++.h>

using namespace std;

long long gcd (long long a, long long b) {
	while (b) {
		a %= b;
		swap (a, b);
	}
	return a;
}

int main(){
    #define int long long
    ios::sync_with_stdio(0);
    int la, ra, ta;
    int lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    if (tb < ta){
        swap(ta, tb);
        swap(la, lb);
        swap(ra, rb);
    }
    int la2 = lb;
    int lb2 = la;
    int ra2 = rb;
    int rb2 = ra;
    int ta2 = tb;
    int tb2 = ta;
    int d = gcd(ta, tb);
    int    sta = (lb / d * d) + la % (d);
    if (sta < lb)
        sta += d;
    int rsta = sta + (ra-la);
    if (rsta > rb){
        rsta = rb;
    }
    int res2 = 0;
    int res1 = max(rsta - sta +1, (long long)0);

    la = la2;
    ra = ra2;
    rb = rb2;
    lb = lb2;
    tb = tb2;
    ta = ta2;
    sta = (lb / d * d) + la % (d);
    if (sta < lb)
        sta += d;
    rsta = sta + (ra-la);
    if (rsta > rb){
        rsta = rb;
    }
    res2 = max(rsta-sta+1, (long long)0);

    cout << max(res1, res2);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll intersect(ll l1, ll r1, ll l2, ll r2) {
    return max(0LL,min(r1,r2)-max(l1,l2)+1);
}

ll solve(ll la, ll ra, ll lb, ll rb, ll gc) {
    lb -= la;
    rb -= la;
    ra -= la;
    la = 0;

    lb += 1000000000*gc;
    rb += 1000000000*gc;

    assert(lb >= 0);
    ll minus = lb - (lb%gc);
    lb -= minus;
    rb -= minus;
    //printf("[%d, %d], [%d, %d]\n",la,ra,lb,rb);
    return intersect(la,ra,lb,rb);
}

int main() {
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll la, ra, ta; cin >> la >> ra >> ta;
    ll lb, rb, tb; cin >> lb >> rb >> tb;
    ll gc = __gcd(ta,tb);

    ll ans = solve(la,ra,lb,rb,gc);
    ans = max(ans,solve(lb,rb,la,ra,gc));
    cout << ans << '\n';
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fo(i,a,b) for(int i=a;i<b;i++)
#define fo_(i,a,b) for(int i=a;i>b;i--)
#define M(a) memset(a,0,sizeof a)
#define M_(a) memset(a ,-1,sizeof a)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265
#define nmax 100100
const ll inf = std::numeric_limits<ll>::max();
const ll mod = 1e9+7;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll md(ll a,ll d){
    return (a%d+d)%d;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll la,ra,ta;
    ll lb,rb,tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    ll tola,tolb;
    tola = ra - la+1LL;
    tolb = rb - lb+1LL;
    ll g = (ll)__gcd(ta,tb);
    ll ans = md(lb-la,g);
    ans = (ll)min(tola,tolb+ans)-ans;
    ans = (ll)max(ans,(ll)min(tola+md(la-lb,g),tolb) - md(la-lb,g));
    cout << max(ans,0LL);

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=3e5+7;

struct state
{
    ll l,r,t;
}a,b;

ll overlap( ll a , ll b , ll c , ll d )
{
    //cout<<a<<" "<<b<<" "<<endl<<c<<" "<<d<<endl;
    return max( 0ll , min(b,d)-max(a,c)+1 );
}

int main()
{
    ll ans=0;
    cin>>a.l>>a.r>>a.t;
    cin>>b.l>>b.r>>b.t;
    ll g = __gcd(a.t,b.t);
    ll d = abs(a.l-b.l);
    d%=g;
    //cout<<overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-a.r+1 ) )<<endl;
    if ( a.l>b.l )
    ans = max( ans , overlap( b.l+d , b.l+d + (a.r-a.l) , b.l , b.r ) );
    else
    {
        ans = max( ans , overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-b.l ) ) );
    }
    d-=g;
    d*=-1;
    if ( a.l>b.l )
    ans = max( ans , overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-b.l ) ) );
    else
    {
        ans = max( ans , overlap( b.l+d , b.l+d + (a.r-a.l) , b.l , b.r ) );
    }

    cout<<ans;

    return 0;
}

#include <bits/stdc++.h>
#define int long long
#define float long double
using namespace std;

void dota47()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    #ifdef _47
        freopen("in.cpp", "r", stdin), freopen("out.cpp", "w", stdout);
    #endif
}

const int MAX = 3e5 + 47;

struct people
{
    int l, r, t, len;
};

people a, b;

int d;

int merge(int LA, int LB)
{
    int RA = LA + a.len;
    int RB = LB + b.len;
    int LMAX = max(LA, LB);
    int RMIN = min(RA, RB);
    return max(0ll, RMIN - LMAX + 1);
}

signed main()
{
    dota47();
    cin >> a.l >> a.r >> a.t, a.len = a.r - a.l;
    cin >> b.l >> b.r >> b.t, b.len = b.r - b.l;
    d = __gcd(a.t, b.t);
    if (a.l > b.l) swap(a, b);
    if (a.l < b.l) a.l += ((b.l - a.l - 1) / d + 1) * d;
    int res = 0;
    for (int k = 0; k <= 1; k++) res = max(res, merge(a.l, b.l + k * d));
    cout << res;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long

int n;
bool p1[2005],p2[2005];
int x1[2005],x2[2005];
vector<vector<int>> g(1005);

int dfs(int v)
{
    if (x1[v]) return v;
    for (int i=0;i<g[v].size();i++)
        return dfs(g[v][i]);
    return -1;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long la,ra,ta,lb,rb,tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;
    if (tb<ta) {swap(la,lb); swap(ra,rb); swap(ta,tb);}
    ll nsd=__gcd(ta,tb);
    ll d=(abs(lb-la))%nsd;
    ll lb1=la+d,rb1=la+rb-lb+d;
    ll ans=min(ra-la,min(rb1-lb1,min(ra-lb1,rb1-la)))+1;
    la+=nsd; ra+=nsd;
    ll ans1=min(ra-la,min(rb1-lb1,min(ra-lb1,rb1-la)))+1;
    la-=2*nsd; ra-=2*nsd;
    ll ans2=min(ra-la,min(rb1-lb1,min(ra-lb1,rb1-la)))+1;
    if (max(max(ans,ans1),ans2)>0) cout<<max(max(ans,ans1),ans2);
    else cout<<0;
    return 0;
}

#include <iostream>
#include<bits/stdc++.h>
#define PI 3.1415926535
#define int long long
//std::cout << std::fixed;
//std::cout << std::setprecision(10);
//std::cout << ans;
using namespace std;
int32_t _gcd(int ta,int tb){
    if(tb==0)
        return ta;
    return (tb,ta%tb);
}
int inter(int la,int lb,int ra,int rb){
    ra=min(ra,rb);
    lb=max(la,lb);
    if(ra-lb<0)
        return 0;
    else
        return ra-lb+1;
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int la,ra,ta;
	int lb,rb,tb;
	cin>>la>>ra>>ta>>lb>>rb>>tb;
	int mind=min(ra-la,rb-lb);
	if(mind==ra-la){
	    swap(ra,rb);
	    swap(la,lb);
	    swap(ta,tb);
	}
	//[la-ra is the bigger interval]
	int32_t Q=__gcd((int32_t)ta,(int32_t)tb);
	int fi=((lb-la)%Q+Q)%Q;
	int ans=inter(0,fi,ra-la,fi+rb-lb);

	for(int C=-1;C<=1;C++){
	    ans=max(ans,inter(0,fi-Q*C,ra-la,fi+rb-lb-Q*C));
	}
	cout<<ans;
	return 0;
}

/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";

long long gcd(long long a, long long b) {
    return (a == 0 ? b: gcd(b % a, a));
}

long long check(long long la, long long ra, long long ta, long long lb, long long rb, long long tb) {
    long long f = gcd(ta, tb);
    long long x = lb;
    long long len = ra - la + 1;
    long long k = lb;
    if (k % f != la % f) {
        if (k % f <= la % f) {
            k += la % f - k % f;
        } else {
            k += la % f + f - k % f;
        }
    }
    long long res = 0;
    for (long long s = k - f; s <= k + f; s += f) {
        chkmax(res, min(s + len - 1, rb) - max(s, lb) + 1);
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //read(FILENAME);
    long long la, ra, ta;
    cin >> la >> ra >> ta;
    long long lb, rb, tb;
    cin >> lb >> rb >> tb;
    long long ans = 0;
    chkmax(ans, check(la, ra, ta, lb, rb, tb));
    chkmax(ans, check(lb, rb, tb, la, ra, ta));
    cout << ans << '\n';
    return 0;
}

#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, la, lb, ra, rb, ta, tb;

int inter(){
	return max(-1, min(ra, rb) - max(la, lb)) + 1;
}

int main(){
    #ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int g = __gcd(ta, tb);
    if(la > 0 and lb > 0){
    	int m = min(la, lb);
    	la -= m;
    	lb -= m;
    	ra -= m;
    	rb -= m;
    }
    if(la != 0){
    	swap(la, lb);
    	swap(ra, rb);
    	swap(ta, tb);
    }
    int d = lb;
    d %= g;
    rb -= (lb - d);
    lb = d;
    //cout << la << " " << ra << " " << lb << " " << rb << endl;
    int ans = inter();
    lb -= g;
    rb -= g;
    lb = max(-1, lb);
    rb = max(-1, rb);
   // cout << la << " " << ra << " " << lb << " " << rb << endl;
    ans = max(ans, inter());
    cout << ans;

}

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define MAXN 200000
#define INF 0x3f3f3f3f

using namespace std;

typedef long long ll;
typedef long double ld;

int a[MAXN];

int gcd(int a, int b) {
  return b == 0 ? a : gcd(b, a % b);
}

int main() {
  int la, ra, ta; scanf("%d %d %d\n", &la, &ra, &ta);
  int lb, rb, tb; scanf("%d %d %d\n", &lb, &rb, &tb);

  if(ta > tb) {
    swap(la, lb); swap(ra, rb); swap(ta, tb);
  }

  ll g = gcd(ta, tb);

  ll ka = (lb - la + g - 1) / g;
  ll lak = la + ka * g;
  ll rak = ra + ka * g;
  ll best = max(0LL, min(rak, (ll) rb) - max(lak, (ll) lb) + 1);

  lak = la + (ka - 1) * g;
  rak = ra + (ka - 1) * g;
  best = max(best, min(rak, (ll) rb) - max(lak, (ll) lb) + 1);

  printf("%lld\n", best);
  return 0;
}

#include <iostream>
#include <numeric>

using namespace std;
typedef long long ll;

// ll singleanswer(ll l, ll r) {
//     return (r-l+1);
// }

int main() {
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;

    ll d = gcd(ta,tb);

    ll ans=0;
    ll dist = abs( (la%d) - (lb%d) );
    if (d==1 || dist==0) {
        //then we can get la+K_a*ta = lb+K_b*tb
        //to coincide;

        ans = min(ra-la+1,rb-lb+1);
    } else {
        //d>2;

        //pretend to put one at 0, the other at dist;

        ll l1,r1,t1;
        ll l2,r2,t2;

        l1=la,r1=ra,t1=ta;
        l2=lb,r2=rb,t2=tb;

        if ( (la%d) > (lb%d) ) {
            swap(l1,l2);
            swap(r1,r2);
            swap(t1,t2);
        }

        //l1 starts at l1%d < l2%d where l2 starts
        //how much overlap?
        ll start1 = max((l1%d),(l2%d));
        ll end1 = min( (l1%d)+(r1-l1), (l2%d)+(r2-l2));
        ll cand1 = max((ll)0, end1-start1+1);

        ll start2 = max( (l2%d), d+(l1%d) );
        ll end2 = min( (l2%d)+(r2-l2), d+(l1%d) + (r1-l1));
        ll cand2 = max((ll)0,end2-start2+1);

        ans = max(cand1,cand2);
    }

    cout<<ans<<endl;

}

#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int MOD=1e9+7;
LL f(LL l1,LL r1,LL l2,LL r2)
{
	return max(0LL,min(r1,r2)-max(l1,l2)+1);
}
int main()
{
	LL la,ra,ta,lb,rb,tb;
	while(cin>>la>>ra>>ta>>lb>>rb>>tb)
	{
		la++,ra++,lb++,rb++;
		LL d=__gcd(ta,tb);
		LL ans=0;
		if(rb==ra)ans=f(la,ra,lb,rb);
		else if(ra<rb)
		{
			LL t=(rb-ra)/d;
			la+=t*d;
			ra+=t*d;
			//不一定刚好对齐，所以在不多出一点和多出一点选最大
			ans=f(la,ra,lb,rb);
			ans=max(ans,f(la+d,ra+d,lb,rb));
		}
		else if(ra>rb)
		{
			LL t=(ra-rb)/d;
			lb+=t*d;
			rb+=t*d;
			ans=f(la,ra,lb,rb);
			ans=max(ans,f(la,ra,lb+d,rb+d));
		}
		cout<<ans<<endl;
	}
}

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;a++)
#define FORD(a,b,c) for(int a=b;a>=c;a--)
using namespace std;
long long gcd(long long x, long long y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}
long long l1, r1, t1, l2, r2, t2;
long long ceill(long long x, long long y) {
    if (x > 0) return x / y;
    x = -x;
    x += (y - 1);
    x /= y;
    return -x;
}
int main(){
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &l1, &r1, &t1, &l2, &r2, &t2);
    long long g = gcd(t1, t2);
    long long tt1 = r1 - l1 + 1, tt2 = r2 - l2 + 1;
    long long tt = max(tt1, tt2), ttt = min(tt1, tt2);
    long long kk = ceill(tt - r1 + l2 - 1, g);
    long long ans = kk * g + r1 - l2 + 1;
    kk = ceill(tt - r2 + l1 - 1, g);
    ans = max(ans,  kk * g + r2 - l1 + 1);
    printf("%I64d\n", max(0LL, min(ttt, ans)));
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

/*
339403920 743344311 1000000000
1 403940389 403940390

*/

int main(){
	long long la,ra,ta;
	cin>>la>>ra>>ta;
	long long lb,rb,tb;
	cin>>lb>>rb>>tb;

	if(ta>tb){
		swap(la,lb);
		swap(ra,rb);
		swap(ta,tb);
	}

	long long g=__gcd(ta,tb);
	if(g==1){
		long long mini=min(ra-la,rb-lb)+1;
		cout<<mini;
		return 0;
	}

	long long len1=ra-la+1;
	long long len2=rb-lb+1;
	ra-=la;
	lb=(lb-la+tb)%tb;
	rb=(rb-la+tb)%tb;
	la=0;
	lb%=g;

//	cout<<lb<<endl;
	if(lb==0){
		cout<<min(len1,len2);
		return 0;
	}

	long long k=(tb-lb+g-1)/g;
	long long st=lb+(k-1)*g;
	long long en=(st+len2-1+tb)%tb;
//	cout<<st<<" "<<en<<endl;
	long long ans=0;
	if(en<st){
		long long add=(st<=ra)?(ra-st+1):0;
		ans=max(ans,add+min(en+1,ra+1));
	}
//	cout<<ans<<endl;
	if(ra>=lb){
		long long st1=lb;
		long long en1=(st1+len2-1+tb)%tb;
		if(st1>en1){
			ans=max(ans,ra-st1+1+en1+1);
		}
		else{
			ans=max(ans,ra-st1+1);
		}
	}

	cout<<ans;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
int read() {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
	return x * f;
}

ll cal(ll l1, ll r1, ll l2, ll r2) {return max(0ll, min(r1, r2) - max(l1, l2) + 1);}
ll l1, r1, t1, l2, r2, t2;
ll gcd(ll a, ll b) {return b? gcd(b, a % b) : a;}
signed main() {
	l1 = read(), r1 = read(), t1 = read(), l2 = read(), r2 = read(), t2 = read();
	ll d = gcd(t1, t2), ans = 0;
	if(r1 == r2) ans = cal(l1, r1, l2, r2);
	else {
		if(r1 > r2) swap(l1, l2), swap(r1, r2), swap(t1, t2);
		ll x = (r2 - r1) / d;
		l1 += x * d, r1 += x * d;
		ans = max(cal(l1, r1, l2, r2), cal(l1 + d, r1 + d, l2, r2));
	}
	printf("%lld\n", ans);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a,ll b){
    return b == 0 ? a : gcd(b,a%b);
}

int main(){
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;
    ll Lena = ra - la + 1;
    ll Lenb = rb - lb + 1;
    ll g = gcd(ta,tb);
    ll _g = g;
    // cout<<"g = "<<g<<endl;
    ll ans = 0;
    if(la == lb){
        ans = max(ans,min(Lena,Lenb));
    }
    if(la < lb){
        ll sub = (lb - la) % _g;
        ans = max(ans,min(Lena - sub,Lenb));
        sub = _g - sub;
        ans = max(ans,min(Lena,Lenb - sub));
    }
    if(la > lb){
        ll sub = (la - lb) % _g;
        ans = max(ans,min(Lena,Lenb - sub));
        sub = _g - sub;
        ans = max(ans,min(Lena - sub,Lenb));
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll la, ra, ta, lb, rb, tb;

int main() {
	scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &la, &ra, &ta, &lb, &rb, &tb);
	if( ta == tb) {
		ll L = max(la, lb), R = min(ra, rb);
		printf("%I64d\n", max(0LL, R-L+1)); return 0;
	}
	ll gc = __gcd(ta, tb);
	if( gc == 1) return printf("%I64d\n", min(ra-la,rb-lb)+1), 0;
	if( ta > tb) swap(la, lb), swap(ra, rb), swap(ta, tb);
	ll ans = 0, L, R;
	if( la > lb) L = la - ((la - lb) / gc + 1) * gc, R = L + ra -la;
	else L = la + ((lb - la + 1) / gc) * gc, R = L + ra - la;
	ans = max(ans, min(R, rb) - max(L, lb) + 1);
	L += gc; R += gc;
	ans = max(ans, min(R, rb) - max(L, lb) + 1);
	printf("%I64d\n", ans);
	return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
// PEACE
int l1, r1, t1, l2, r2, t2;

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> l1 >> r1 >> t1 >> l2 >> r2 >> t2;
	int ll = l1;
	int rr = r1;
	int tt = t1;
	int lll = l2;
	int rrr = r2;
	int ttt = t2;
	int l = r1 - l1 + 1;
	int g = gcd(t1, t2);
	l1 = l1 % g;
	int x = (l2 / g) * g;
	x += l1;
	while(x < l2)
		x = x + g;
	int res1 = r2 - x + 1;
	res1 = min(res1, l);

	l1 = ll;
	l2 = lll;
	r1 = rr;
	r2 = rrr;
	t1 = tt;
	t2 = ttt;

	int L = r2 - l2 + 1;
	int G = gcd(t1, t2);
	l2 = l2 % G;
	int X = (l1 / G) * G;
	X += l2;
	while(X < l1)
		X = X + G;
	int res2 = r1 - X + 1;
	res2 = min(res2, L);
	int ans = max(res1, res2);
	ans = max(ans, 0LL);
	return cout << ans, 0;
}

#include<bits/stdc++.h>
 using namespace std;
 const int N = 1e5 + 10;
const int K = 200;
const int mod = 998244353;
int power(int a, int b){
 int res = 1;
 while(b){
    if(b & 1){
      res = 1LL * res * a % mod;
    }
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return res;
 }
int main(){
long long int la, ra, ta, lb, rb, tb;
scanf("%lld %lld %lld", &la, &ra, &ta); scanf("%lld %lld %lld", &lb, &rb, &tb);
long long int dis;
if(la<=lb){
swap(la,lb);
swap(ra,rb);
swap(ta,tb);
}
dis=abs(la-lb);
long long int gg=__gcd(ta,tb);
long long int ans = ceil(dis/(double)gg);
long long int v = ans * gg - dis;
ans = floor(dis/(double)gg);
long long int v1 = dis-ans * gg;
ra-=la;
rb-=lb;
la=lb=0;
ans=0;
ans=max(ans, min(ra+v1,rb)-max(la+v1,lb)+1);
ans=max(ans, min(ra,rb+v)-max(la,lb+v)+1);
cout<<ans<<endl;
 return 0;
 }

#include <bits/stdc++.h>

using namespace std;

int intersect(int l , int r , int l2 , int r2)
{
    return max(0 , min(r , r2) - max(l , l2) + 1);
}

int main()
{
    long long l , r , t , l2 , r2 , t2 ;
    cin>>l>>r>>t;
    cin>>l2>>r2>>t2 ;
    if(l > l2)
       swap(l , l2) , swap(r , r2) , swap(t , t2) ;
    int gcd = __gcd(t , t2) ;
    if(gcd == 1 || (l2 >= l && r2 <= r) || (l >= l2 && r <= r2))
        return cout<<min(r-l+1 , r2-l2+1)<<"\n" , 0 ;
    int start = (l2 - l) / gcd ;
    return cout<<max(intersect(l + start * gcd , r + start * gcd , l2 , r2) , intersect(l + (start + 1) * gcd , r + (start+1) * gcd , l2 , r2))<<"\n" , 0 ;
}

#include <iostream>

using namespace std;

typedef long long LL;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

inline void swap(LL &a, LL &b) {
    LL t = a;
    a = b;
    b = t;
}

inline LL maxi(LL a, LL b) {return a > b ? a : b;}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    LL la, ra, ta, lb, rb, tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    la++, ra++, lb++, rb++;
    if (rb - lb < ra - la)
        swap(la, lb), swap(ra, rb), swap(ta, tb);
    if (ta == tb) {
        if (la >= lb && ra <= rb)
            cout << ra - la + 1 << "\n";
        else {
            LL ans;
            if (la < lb) {
                ans = ra - lb + 1;
                cout << (ans < 0 ? 0 : ans) << "\n";
            } else {
                ans = rb - la + 1;
                cout << (ans < 0 ? 0 : ans) << "\n";
            }
        }
    } else {
        LL g = gcd(ta, tb);
        // while (la < lb)
        //     la += g, ra += g;
        // while (la >= lb)
        //     la -= g, ra -= g;
        // la += g, ra += g;
        LL diff = ra - la;
        la = ((la - lb) % g + g) % g + lb;
        ra = la + diff;
        // cout << g << endl;
        // cout << la << endl << ra << endl;
        // cout << lb << endl << rb << endl;
        if (la >= lb && ra <= rb) {
            cout << ra - la + 1 << "\n";
        } else {
            LL ans1 = rb - la + 1;
            if (ans1 < 0) ans1 = 0;
            la -= g, ra -= g;
            LL ans2 = ra - lb + 1;
            if (ans2 < 0) ans2 = 0;
            cout << maxi(ans1, ans2) << "\n";
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DFOR(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define fi first
#define se second
#define filename "F."
using namespace std;
const int N=1e5+3;
LL La,Ra,Ta,Lb,Rb,Tb,d;
LL Tinh(LL st)
{
    LL tLa = La + st* d;
    LL tRa = Ra + st* d;
    return max(0ll, min(tRa,Rb) - max(tLa,Lb) +1 ) ;
}
int main()
{
    cin >> La >> Ra >> Ta >> Lb >> Rb >> Tb;
    if(La> Lb ) {
        swap(La,Lb); swap(Ra,Rb); swap(Ta,Tb);
    }
    d = __gcd(Ta,Tb) ;
    LL step = (Lb-La) / d;
    cout << max(Tinh(step),Tinh(step+1)) ;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll l, r, t, l2, r2, t2, d, d2, ans;
void solve(ll off) {
	if(off < 0)
		off += t;
	if(off >= t)
		off -= t;
	assert(0 <= off && off < t);
	ll temp = max(0ll, r-(l+off)) - max(0ll, r-(l+off+d2));
	temp = max(temp, (l+off+d2)-(l+t));
	ans = max(ans, temp);
}
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> l >> r >> t >> l2 >> r2 >> t2;
	if(r2-l2 > r-l) {
		swap(l,l2);
		swap(r,r2);
		swap(t,t2);
	}
	ll g = __gcd(t, t2);
	r++, r2++;
	d = r-l;
	d2 = r2 - l2;
	ll goal = l;
	ll off;
	if(goal > l2) {
		ll diff = goal - l2;
		off = (l2-l) + (diff+g-1)/g*g;
	} else {
		ll diff = l2 - goal;
		off = (l2-l) - (diff)/g*g;
	}
	solve(off);
	solve(off-g);
	cout << ans;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007

ll cal(ll x1, ll y1, ll x2, ll y2){
	// cout << x1 << " " <<y1 << " " << x2 << " " << y2 << "\n";
	return max(min(y1, y2) - max(x1, x2) + 1, 0LL);
}

ll solve(ll la, ll lb, ll ra, ll rb, ll ta, ll tb){
	ll opt = min(rb - lb + 1, ra - la + 1);
	if(la == lb){
		// cout << opt << "\n";
		return opt;
	}
	ll g = __gcd(ta, tb);
	ll rem = lb - la;
	ll shift = rem % g;
	if(shift < 0){
		shift += g;
	}
	ll ans1 = cal(0, ra - la, shift, rb - lb + shift);
	// ll ans2 = cal(g - shift, ra - la + g - shift, 0, rb - lb);
	return max(ans1, 0LL);
}

int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll la, ra, ta;
	cin >> la >> ra >> ta;
	ll lb, rb, tb;
	cin >> lb >> rb >> tb;
	ll fin = solve(la, lb, ra, rb, ta, tb);
	fin = max(fin, solve(lb, la, rb, ra, tb, ta));
	cout << fin << "\n";
	return 0;
}

// In The Name Of God

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
	ll l1,r1,t1,l2,r2,t2;
	cin>>l1>>r1>>t1;
	cin>>l2>>r2>>t2;
	if(l1>l2) swap(l1,l2) , swap(r1,r2) , swap(t1,t2) ;
	ll k=__gcd(t1,t2) , p , ans=0 ;
	p=(l2-l1)/k ;
	l1+=k*p , r1+=k*p ;
	ans=max(ans,(min(r1,r2)-max(l1,l2))+1);
	l1+=k , r1+=k ;
	ans=max(ans,(min(r1,r2)-max(l1,l2))+1);
	cout<<ans;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long mx(long long l1,long long r1,long long l2,long long r2){
    return max(0LL,min(r1,r2)-max(l1,l2)+1);
}
int main(){
    long long l1,r1,t1,l2,r2,t2;
        while(~scanf("%lld%lld%lld%lld%lld%lld",&l1,&r1,&t1,&l2,&r2,&t2)){
        l1++;
        r1++;
        l2++;
        r2++;
        long long mnsuojin=__gcd(t1,t2);
        long long daierta=abs(r1-r2)/mnsuojin*mnsuojin;
        long long ans=0;
        if(r1==r2)
            ans=mx(l1,r1,l2,r2);
        else if(r1<r2){
            r1+=daierta;
            l1+=daierta;
            ans=max(mx(l1,r1,l2,r2),mx(l1+mnsuojin,r1+mnsuojin,l2,r2));
        }
        else if(r1>r2){
            r2+=daierta;
            l2+=daierta;
            ans=max(mx(l1,r1,l2,r2),mx(l1,r1,l2+mnsuojin,r2+mnsuojin));
        }
        printf("%lld\n",ans);
        }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll l, r, t, l2, r2, t2, d, d2, ans;
void solve(ll off) {
	if(off < 0)
		off += t;
	if(off >= t)
		off -= t;
	assert(0 <= off && off < t);
	ll temp = max(0ll, r-(l+off)) - max(0ll, r-(l+off+d2));
	temp = max(temp, (l+off+d2)-(l+t));
	ans = max(ans, temp);
}
int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> l >> r >> t >> l2 >> r2 >> t2;
	if(r2-l2 > r-l) {
		swap(l,l2);
		swap(r,r2);
		swap(t,t2);
	}
	ll g = __gcd(t, t2);
	r++, r2++;
	d = r-l;
	d2 = r2 - l2;
	ll goal = (l+r-d2)/2;
	ll off;
	if(goal > l2) {
		ll diff = goal - l2;
		off = (l2-l) + (diff+g-1)/g*g;
	} else {
		ll diff = l2 - goal;
		off = (l2-l) - (diff)/g*g;
	}
	solve(off);
	solve(off-g);
	cout << ans;
}

#pragma once
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define debug(x) std::cout << #x << ": " << x << '\n';
typedef long long ll;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll inter(ll a, ll b, ll a1, ll b1) {
	return std::max(0LL, std::min(b, b1) - std::max(a, a1) + 1);
}
ll less(ll a, ll b) {
	if (a < 0 && a % b != 0) {
		a += -b - (a % b);
	}
	return a / b;
}

int32_t main() {
	std::ios::sync_with_stdio(false);
	int l, r, t;
	int l1, r1, t1;
	std::cin >> l >> r >> t;
	std::cin >> l1 >> r1 >> t1;
	// ll razn = (t1 - t);
	// if (razn == 0) {
	// 	std::cout << inter(l, r, l1, r1);
	// 	return 0;
	// }
	ll answ = 0;
	ll minLen = std::min(r - l + 1, r1 - l1 + 1);
	ll f1 = -(r1 - l + 1 - minLen);
	if (f1 < 0 && f1 % gcd(t, t1) != 0) {
		f1 += -gcd(t, t1) -(f1 % gcd(t, t1));
	}
	ll answ1 = f1 / gcd(t, t1) * gcd(t, t1) + (r1 - l + 1);
	answ = std::max(answ, answ1);
	f1 = -(r - l1 + 1 - minLen);
	if (f1 < 0 && f1 % gcd(t, t1) != 0) {
		f1 += -gcd(t, t1) -(f1 % gcd(t, t1));
	}
	answ1 = f1 / gcd(t, t1) * gcd(t, t1) + (r - l1 + 1);
	ll d = gcd(t, t1);
	// debug(less(l1 - l, d));
	ll x = less(r - r1, d) * d;
	// debug(x);
	if ((l1 - l) >= -x) {
		std::cout << minLen;
		return 0;
	}
	x = less(r1 - r, d) * d;
	// debug(x);
	// debug(r1 - r);
	if ((l - l1) >= -x) {
		std::cout << minLen;
		return 0;
	}
	answ = std::max(answ, answ1);
	std::cout << answ;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;

ll gcd(ll a,ll b) {
    if(a < b) return gcd(b, a);
    if(b == 0) return a;
    return gcd(b, a%b);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll la, ra, ta;
    ll lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    if(ra-la > rb-lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);

    }
    ra -= la;
    lb -= la;
    rb -= la;
    la = 0;
    ll d = gcd(ta, tb);
    ll md = lb;
    while(md < 0) {
        if(md/d < 0) md += -(d)*(md/d);
        else {
            md += d;
        }

    }
    md = md%d;

    ll ans = 0;
    rb += md-lb;
    lb = md;
    ans += max(min(ra-md+1, ra-la+1), ll(0));
    /*ans += max(((rb-ta)/ta)*(ra-la+1) , ll(0));
    if(rb >= ta) {
        ans += min(((rb)%ta)+1, ra-la+1);
    }*/
    ll ans2 = 0;
   // if(md != 0) {
        md -= d;
        rb += md-lb;
        lb = md;
        ans2 += max(min(ra+1, rb+1), ll(0));
       /* ans2 += max(min(ra-ta-lb+1, ra-la+1), ll(0));
        ans2 += max(((rb-ta)/ta) , ll(0));
        if(rb >= ta) {
            ans2 += min(((rb)%ta)+1, ra-la+1);
        }*/
   // }

    cout << max(ans, ans2) << endl;

}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

LL la,ra,ta,lb,rb,tb;

int main(){
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    LL cnt=__gcd(ta,tb);
    LL xa=ra-la+1,xb=rb-lb+1;
    LL d=la-lb;
    d%=cnt;
    d+=cnt;
    d%=cnt;
    if(d == 0)printf("%lld\n",min(xa,xb));
    else printf("%lld\n",max(1LL*0,max(min(xa,xb-d),min(xb,xa-(cnt-d)))));
    return 0;
}

/**********************************************************
 *     Code written by Akikaze                            *
 *     Duy-Bach Le, #Team4T's Chief Executor              *
 *     #Team4T Tertiary Flagship - Oblivion               *
 *                                                        *
 *     Written by a random fan of momocashew and Chiho    *
 *                                                        *
 *     Arigatougozaimasu, imouto-chan.                    *
 **********************************************************/

/************** [OPTIMIZATION PROTOCOL] **************/
#pragma comment(linker, "/stack:225450978")
#pragma GCC optimize("Ofast")
/*****************************************************/

/************** [LIBRARY PROTOCOL] **************/
#include <bits/stdc++.h>
using namespace std;
/************************************************/

/************** [LEGENDS/CONSTANTS] **************/
#define endl '\n'
#define i64 long long
#define ld long double
#define rsz resize
#define pub push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<i64> vi;
typedef vector<ld> vd;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<i64, i64> pii;
typedef pair<i64, pii> pip;
typedef pair<pii, i64> ppi;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116, EPS = 1e-9, Gold = ((1+sqrt(5))/2);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
long long keyCount = sizeof(keymod) / sizeof(long long);
/*************************************************/

/************** [BITWISE FUNCTIONS] **************/
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcountll(s); }
/*************************************************/

/************** [MASTER CONTROLS - PHASE 1] **************/
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
#define OImode 227420978
#define MultiTest 227420978
#define Interactive 227420978

#undef OImode // Switch this off if submitting OI problems.
#undef MultiTest // Switch this off if submitting multi-test problems.
#undef Interactive // Switch this off if submitting interactive problems. Also required when you need to debug step-by-step.

void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
string cppstr_infile = "FILE.IN";
string cppstr_outfile = "FILE.OUT";
/*********************************************************/

/************** [GLOBAL VARIABLES] **************/
i64 la, ra, ta, lb, rb, tb;
/************************************************/

/************** [FUNCTIONS] **************/
i64 process(i64 g) {
	i64 top = -(LINF*4)/g, bot = (LINF*4)/g, ans = -LINF*8;
	while (top <= bot) {
		i64 mid = (top + bot) / 2;
		if (la + mid * g < lb) {ans = la + mid * g; top = mid + 1;}
		else bot = mid - 1;
	}
	return ans;
}

void Input() {
	cin >> la >> ra >> ta >> lb >> rb >> tb;
	if ((ra - la + 1) > (rb - lb + 1)) {
		swap(la, lb); swap(ra, rb); swap(ta, tb);
	}
}

void Solve() {
	i64 LenA = ra - la + 1;
	i64 g = __gcd(ta, tb);
	i64 closestL = process(g);
	i64 shiftedL = closestL + g;
	cout << max(max(min(closestL+LenA-1, rb) - max(closestL, lb) + 1, 0LL), max(min(shiftedL+LenA-1, rb) - max(shiftedL, lb) + 1, 0LL));
}
/*****************************************/

/************** [MAIN] **************/
int main(int argc, char* argv[]) {
	ControlIO(argc, argv);

	#ifndef Interactive
		ios_base::sync_with_stdio(0); cin.tie(NULL);
	#else
		ios_base::sync_with_stdio(0);
	#endif

	#ifndef MultiTest
		Input(); TimerStart();
		Solve(); TimerStop();
	#else
		int T; cin >> T; TimerStart();
		while(T--) {Input(); Solve();}
		TimerStop();
	#endif

	return 0;
}
/************************************/

/************** [MASTER CONTROLS - PHASE 2] **************/
void ControlIO(int argc, char* argv[]) {
	char* infile = new char[cppstr_infile.size()+1];
	char* outfile = new char[cppstr_outfile.size()+1];
	strcpy(infile, cppstr_infile.c_str());
	strcpy(outfile, cppstr_outfile.c_str());
	#ifdef Akikaze
		time_t t = time(0);
		tm* now = localtime(&t);
		cout << "Source code by #Team4T-Akikaze.\n";
		cout << "Current time: ";
		cout << (now->tm_year + 1900) << '-'
			 << ((now->tm_mon < 9) ? "0" : "") << (now->tm_mon + 1) << '-'
			 << ((now->tm_mday < 10) ? "0" : "") <<  now->tm_mday << " | "
			 << ((now->tm_hour < 10) ? "0" : "") <<  now->tm_hour << ":"
			 << ((now->tm_min < 10) ? "0" : "") <<  now->tm_min << ":"
			 << ((now->tm_sec < 10) ? "0" : "") <<  now->tm_sec
			 << "\n\n";

		#ifdef OImode
			cout << "OI-Mode: ON\n";
			cout << "Input file: " << infile << endl;
			cout << "Output file: " << outfile << endl;
			cout << "\n";
		#else
			cout << "OI-Mode: OFF\n";
			cout << "Input file: NULL\n";
			cout << "Output file: NULL\n";
			cout << "\n";
		#endif

		#ifdef MultiTest
			cout << "MultiTest-Mode: ON\n";
		#else
			cout << "MultiTest-Mode: OFF\n";
		#endif

		#ifdef Interactive
			cout << "Interactive-Mode: ON\n\n";
		#else
			cout << "Interactive-Mode: OFF\n\n";
		#endif

		if (argc > 1) assert(freopen(argv[1], "r", stdin));
		if (argc > 2) assert(freopen(argv[2], "w", stdout));
	#elif OImode
		freopen(infile, "r", stdin);
		freopen(outfile, "w", stdout);
	#endif
}

void TimerStart() {
	#ifdef Akikaze
		TimeStart = chrono::steady_clock::now();
	#endif
}

void TimerStop() {
	#ifdef Akikaze
		TimeEnd = chrono::steady_clock::now();
		auto ElapsedTime = TimeEnd - TimeStart;
		cout << "\n\nTime elapsed: " << chrono::duration<double>(ElapsedTime).count() << " seconds.\n";
	#endif
}

void Exit() {
	TimerStop(); exit(0);
}
/*********************************************************/

/*********************************************************************************************************************************************************************************************************
 * ti;.                               .:,:i:         :,;;itt;.        fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE      :,:.    f::::. .,ijLGDDDDDDDEEEEEEE*
 *ti;.                               .:,:i:          .:,;itt;:      GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG.     .::.      f:,...,ijLGDDDDDDDDEEEEEE *
 *ti;.                               .:,:i:            :,;;iti,    :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG     .::.       .f,::,ijLGDDDDDDDDEEEEEE *
 *ti;.                               .:,:i:             .,,;iti;.  LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj    .::.        .:L;;ijfGDDDDDDDDDEEEEE *
 *ti;.                               .:,:i:              .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWKWWWWWWWWWWWWWWKKKKKKKEL    .::.         .:;LijLGGDDDDDDDDEEEEE *
 *ti;.                               .:,:;:                :,;;ittfDEEEEEEEEEEEEEEEEKEEEGEEEEEEEEEEEKWWWEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWKKKKKKKELj   .::.          :,;jffGGDDDDDDDDDEEEE *
 *ti;.                               .:,:i:                 .,;;tGGDEEEEEEEEEEEKEEEKEEEDEEEEEEEEEEEEWWWEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWKWWWWWWKKKKKKKEEL   .::.          .:;itDGGDDDDDDDDDEEEE *
 *ti;.                               .:::;:                   :;ifDEEEEEEEEEEEEKEEEKEEEEEEEEEEEEEEEWWWEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WWWKKKKKKKKEEf   .::.           :,itfGEDDDDDDDDDDDEE *
 *ti;.                               .:::;:                    :GGEEEEEEEEEEEKEKEEKEEEEEEEEEEEEEEEEWWEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKKEEDG  .::.           .,;jfLGKDLDDDDDDEEDD *
 *ti;.                               .:::;:                     fDEEEEEEKKKKKKKKKEKEEEEEEEEEEEEEEE#WEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#KKKKKKKKKKEEf .:::.           .,;tfLGDEDDDDDDDDEEE *
 *ti;.                                :::;:                    fDEEEEEEKKKKKKKKKKWKEEEEEEEEEEEEEEEWKEEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKKKEEft :::.           .,;tfLGDDDKDDDDDDDDD *
 *ti;.                                .::;:                    fDEEEEEEKKKKKKKWKKKKKEEEEEEEEEEEEE#WEEWEEEEEDEEDEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKEEGG :,:.           .,;tfLGGDDDKDDDDDDDD *
 *ti;.                                .:.;:                   tGDEEEEKKKKKKKKKKKKKKKKKEEEEEEEEEEEWEEKWEEEEEEEDEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKKKKKEEDf :::.           .,;tfLGGDDDDEDDDDDDD *
 *ti;.                                .::;:                   fDEEEEEKKKKKKKKKKKWKKKKKKKKEEEEEEEWWEEWEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKEEEft.::.           .,;tfLGGDDDDDDEDDDDD *
 *ti;.                                .:.;:                  tGDEEEKKKKKKKKKKKKKKKKKKKKKKEKEEEEE#EEWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKEEEGD:::.           .,;tfLGGDDDDDDDEDDDD *
 *ti;.                                .:.,.                  LDEEEEKKKKKKKKKKWKWKKKKKKKKKKKKEEEKWEKW#EEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKW##KKKKKKEEEEf,,:.           .,;tfLGGDDDDDDDDEDDD *
 *ti;.                               ..:.,.                 LGDEEEEKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEEW#WEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKK##KKKKKEEEEEfi;,.           .,;tfLGGDDDDDDDDDKDD *
 *tt;.                                .:.,:                 jDEEEEKKKKKKKKKKWWKKKKKKKKKKKKKKKKKWKE#WWEEEEEEEEEEEEEEWEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKWWKKKKEEEEDfG;,:           .,;tfLGGDDDDDDDDDDKD *
 *tii,.                               .:.,.                tGDEEEEKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKKWWWKEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKW#KKKKEEEEDGGi;,.          .,;tfLGGDDDDDDDDDDDE *
 *ti;;,:.                             .:.,:                fDEEEEKKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEK#WWKEEEEEEEEEEEEDEEEEEEEEEEEEEEGEEEEEEEEEEEEEEEEEEEKKKKKKKWWKKEEEEEEDDf;;;,.         .,;tfLGGDDDDDDDDDDDD *
 *tii;,,:..                           ...,.               ;LEEEEEKKKKKKWKKKKWKKKKKKKKKKKKKKKKKEKKW#WEEEEEEEEEEEEEjEEEEEEEEEKEEEEGEEEEEEEEEKEEEEEEEEEEEEEEEEE#WKEEEEEEDDf;;;;,:        .,itfLGGDDDDDDDDDDDD *
 *ti;,,,,,:.                          ...,.               LDEEEEKKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKK#W#WEEEEEEEEEEEDDLEEEEEEEEEWEEEEDEEEEEEEEEKEEEEEEEEEEEEEEEEEWWEEEEEEEDDfj,,,,,:.      .,itfGGGDDDDDDDDDDDD *
 *tii,,,,::::.                        ...,:              .fDEEEEKKKKKKWKKKKWWWKKKKKKKKKKKKKKKEKKW#WWEEEEEEEEEEEKiKEEKEEEEEEWEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEEWWEEEEEEEDDLD:::,,,:.     .,ijfGGGDDDDDDDDDDDD *
 *ti;:::::::::..                      .:.,:              LDEEEEKKKKKKKWKKKKWWKKKKKKKKKKKKKKKKtKKWWWWKEEEEEEEEEDiiDEEEEEEEEWWEEEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEWKEEEEEDDDGL:. .:,,,:    .,ijLGGGDDDDDDDDDDDD *
 *tt;. .::::::::..                    ...,:             :fDEEEKKKKKKKKKKKKWW#KKKKKKKKKKKKKKKKfKKWWWWKEEEEEEEEDti,DEKEEEEEEWWEEEDEEEEEEEEEKEEEEEEEEEEEEEDEEEEE#WEEEEEGGDGf:.  .:,;,:.  .,ijLGGDDDDDDDDDDDDD *
 *tt;.   .:::::::..                   ...,:             GDEEEKKKKKKKKWKKKKWWWKKKWKKKKKKKWWWKDEKLWWWWKKEEEEEEDEi,LDEEEEEEEEWWEEEEEEEEEEEEEEEEEEEEEEEEEDEDEEEEEW#EEEEDDDDGf,.    :,,,:...,ijLGGGDDDDDDDDDDDD *
 *tt;.    .....::::..                 ...,:             fDEEEKKKKKKKKWKKKKWWWWKKKWKKKKKKKKKKfWKiWWW#KKEEEEEEEi;.EDfEEEDEEiWWEEEEEEEEEEEEDGKEEEEEEEEEEDEEEEEEEWWEEEEDDDGGLi.     .,;,:::,ijLGGGDDDDDDDDDDDD *
 *tt;.      ....:::::.                ...,.            iDEEEEKKKKKKKKWKKWKWWWWWKKWWWKKKKKKKKtWKt#WWWKKEEEEEDji..DDKDDEDEGiWKEEEEEEEEEEDDEjEEEEEEEEEEEDEEEEEEEKWKEEDDDDGGff.      .:,;,,;ijLGGGDDDDDDDDDDDD *
 *tt;.        ....::::..              .:.,:           .LDEEEKKKKKKKKKKKKWWWWKWWWWWWWWWWKKKKWtKKiDWWWKKKEEEEKi:..DEDDDDDDiiWKEEEEEEEEEEDDEijDEEEEEKEEEEEEEEEEEEWWEEGDDDGGLG.       .:,;;iijLGGGDDDDDDDDDDDD *
 *tt;.          .....:::..            ...,.           .fEEEEKKKKKKKKWKKKKWWWWWWWWWWWWWWKWKKKiKDiLWWWWKEEEEEi,..fD:DDDDDti;WEEEEEEEEEEKDDi:iDDEEEEWEEEEEEEEEEEE#WEEGDDDDGGG.         :,iitjLGGGDDDDDDDDDDDD *
 *tti.            .....:::..          ...,.           GDEEEKKKKKKKKKWKKKWWW#WWWWWWWWWWWKWKKjiEjitWWWKKWEEEDi...DDLDDDDji;;WEEEEEEEEEEEDEj.iDDEEEEWEEEEEEEEEEEEWWEEDDDDDDGf.          .,;tjfLGGDDDDDDDDDDDD *
 *tti.              ....::::..        ...,.           fEEEKKKKKKKKKKKKKKKW#WWWWWWWWWWWWWWWWtiEiiiWWWKKEWKEi....D.EDDDEi;.fWEEEEEEEEEEDDfL.;EDDEEEWEEEEEEEEEEEEWWEEEDDDDDGf.           :;ijfLGGDDDDDDDDDDDD *
 *tti.                ....::::..      ...,.          LDEEEKKKKKKKKKKKKKKWWWWWWWWWWWWWWWW####WKiiiWWWKKKEEK,...:E:DDDEii..GWEEEEEEEEDWDDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf:           .,itfLGGDDDDDDDDDDDD *
 *tti.                 .....:::..     ...,.          fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi           .,;tfLGGGDDDDDDDDDDD *
 *tti.                   .....:::..   ...,.         iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL           .,;tjLLGGDDDDDDDDDDD *
 *tti.                     ....::::......:.         LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG           .,;tjLLGGDDDDDDDDDDD *
 *tti.                        ...::::::..,.        :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL           .,;tjLLGGDDDDDDDDDDD *
 *tti.                         ....::::::,.        GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf           .,;tjLLGGDDDDDDDDDDD *
 *tti.                           ....:::,,.       .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf           .:;tjLLGGGDDDDDDDDDD *
 *tti.                             ...::,,,:.     GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf           .:;tjLLGGDDDDDDDDDDD *
 *jti.                              ...::,,,,:.  .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf:          .,;tfLLGGDDDDDDDDDDD *
 *tti.                               ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi          .,;tfLLGGGDDDDDDDDDD *
 *tti.                               ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL          .,;tfLGGGDDDDDDDDDDD *
 *jti.                                ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG          .,;tfLGGGGDDDDDDDDDD *
 *jti.                                ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL          .,;tfLGGGGDDDDDDDDDD *
 *jti.                                ...:.  .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf          .,;tfGGGGDDDDDDDDDDD *
 *jti.                                ...:.    ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi  EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:.        .,;tfGGGGDDDDDDDDDDD *
 *jti.                                ...:.    GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff     .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,:       .,;tfGGGGDDDDDDDDDDD *
 *jti.                                ...:.    GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK,   ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:.     .,;tfGGGDDDDDDDDDDDD *
 *jti.                                ...:.  .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:.   .,;tfGGGDDDDDDDDDDDD *
 *jti.                                ...:.   DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD *
 *jti.                                ...:.  fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG  .:,,,:::,;tfGGGDDDDDDDDDDDD *
 *jti.                                ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL    .:,;,,,;tfGGGDDDDDDDDDDDD *
 *jti.                                ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf     .:,;;;itfGGGDDDDDDDDDDDD *
 *jti.                                ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf       .,;iitfGGGDDDDDDDDDDDD *
 *jti.                                ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL        .:;itfGGGDDDDDDDDDDDD *
 *jji.                                ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j.  KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf         .,;tfGGGDDDDDDDDDDDD *
 *jji.                                ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;..................................  . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL,        .:;tfGGGDDDDDDDDDDDD *
 *jji.                               .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................::  KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj        .:;tfGGDDDDDDDDDDDDD *
 *jji.                             .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG        .:;tfGGGDDDDDDDDDDDD *
 *jji.                          . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG     .;tjfLLGDDDDDDDDDDDDDDD *
 *jji.                          i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL  ,fGGGDDDDDDDDEEEDDDDDDDDDD *
 *jji.                        .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD *
 *jji.                       i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD *
 *jji.                     .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD *
 *jji.                    .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL;   .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD *
 *jji.                   :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED *
 *jji.                  i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE *
 *jji.                 i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD *
 *jji.                .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG *
 *jjt.                :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG *
 *jji.               ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG *
 *jjt.              f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG *
 *jjt.             ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG *
 *fji.             ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL *
 *fji.            t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL *
 *fjt:            .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL *
 *fjji;:.       .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG *
 *fjjtii;,:.     :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG *
 *fjtiiiii;;:.  j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG *
 *fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG *
 *fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD *
 *jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD *
 *fjt.    .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD *
 *fjt.       .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD *
 *fjt.       .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD *
 *fjt:        :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD *
 *fjt:       t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD *
 *jjt.       f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD *
 *fjt:.      ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG *
 *fjt:.     ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; *
 *fjt:.     f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, *
 *fjt:.    .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWWGi;i;,..;jDDDKEGi;;;;;;:................EED,,,,,,D,,,,,,,,,,,,,,,,,,,,,,,,, *
 *fjt:.  .. ;::,,,,,;;EDDEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKW#WW####KWKKKiiiiiiiiiiiiijKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWt;i;;;;i;DDDDDDGi;;;;;;;;:.............EDf;,,,;,G;;;;;;;;;;;;;;;,,,,,,,,,, *
 *fjt:......:,,,,,,;LDDDEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWWKWWWW####KKKKKiiiiiiiiiiijKEKKWKKKKKKKWWWWWWWWWWWWWWWWWWWWWWiLiii;i;DEEEEDDE;i;;;;;;;;;:..........EDi,;;;;;L;;;;;;;;;;;;;;;;;;,,,,,,, *
 *fjt:......:,,,,,;EDDDEEKEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKWWWWKKWWW##W#KWKKWEiiiiiijGKKKKKWWKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKi;iiiiDDEEEEEEDEi;;;;;;;;;;;;;,:.....ED;;;;;;;j;;;;;;;;;;;;;;;;;;;;;;;,, *
 *fjt:.....t,,,,,;DDDDEEEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKWWWW##WKWKKWKiiiKKKKKKKKKWWKKKKKKKKWWWWWWWWWWWWWWW#WWWWWWWWWiiiiifLEEEEEEEEDi;i;;;;;;;;;;;;.....DD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; *
 *fjt:.....G,,,,,GDDDEEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKKWWWKKKWWW###WKWKKWKitKKKKKKKKKWKKKKKKKKKKWWWWWWWWWWWWWW###WWWWWWWWEiiiiiiiEEEEEEEEDGiiii;;;;;;;;;.....GD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; *
 *fjt:.....L,,,,;GDDDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; *
 ***********************************************************************************************************************************************************************************************************/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>>(h, vector<type>(w)) v; rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); solve(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

ll intersect(ll la, ll ra, ll lb, ll rb) {
  if (la > lb) {
    swap(la, lb);
    swap(ra, rb);
  }
  return max(0LL, min(ra, rb) - lb + 1);
}

ll solve(ll la, ll ra, ll ta, ll lb, ll rb, ll tb) {
  ll g = __gcd(ta, tb);
  ll inf = 1e9;
  lb += g * inf, rb += g * inf;
  ll diff = lb - la;
  la += (diff / g) * g;
  ra += (diff / g) * g;
  ll ans = 0;
  rep(i, 0, 2) {
    // dbg(la, ra, lb, rb);
    chmax(ans, intersect(la, ra, lb, rb));
    la += g, ra += g;
  }
  // dbg(la, ra, lb, rb, g);
  return ans;
}

void solve() {
  rd(ll, la, ra, ta);
  rd(ll, lb, rb, tb);
  wr(max(solve(la, ra, ta, lb, rb, tb), solve(lb, rb, tb, la, ra, ta)));
}

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int main(){
    ll la,ra,ta,lb,rb,tb,temp,tatti,res,tatti2,res2;
    scanf("%lld %lld %lld",&la,&ra,&ta);
    scanf("%lld %lld %lld",&lb,&rb,&tb);
    temp = gcd(ta,tb);
    if(la <= lb){
        tatti = (lb-la)%temp;
        res = min((ra-la+1) - tatti,(rb-lb)+1);
        res2 = min((rb-lb)+1 - (temp - tatti),(ra-la)+1);
        res = max(res,res2);
        res = max(res,0ll);
    }
    else{
        tatti = (la-lb)%temp;
        res = min((rb-lb+1) - tatti,(ra-la)+1);
        res2 = min((ra-la)+1 - (temp - tatti),(rb-lb)+1);
        res = max(res,res2);
        res = max(res,0ll);
    }
    cout<<res;
    // tatti = min(ra - la + 1,rb - lb + 1);
    // tatti2 = max(ra - la + 1,rb - lb + 1);
    // res = tatti - (abs(lb-la)%temp);
    // res2 = min(tatti2 - (temp - (abs(lb-la)%temp)),tatti);
    // res = max(res,res2);
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
//char buf[1<<15],*S=buf,*T=buf;
//char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read(){
	int x=0,f=1;char ch;ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
	if(f)return x;else return -x;
}
int l[2],r[2],t[2];
int len[2];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main()
{
	l[0]=read();r[0]=read();t[0]=read();
	len[0]=r[0]-l[0]+1;
	l[1]=read();r[1]=read();t[1]=read();
	len[1]=r[1]-l[1]+1;
	int g=gcd(t[0],t[1]);
	int s=((l[1]-l[0])%g+g)%g;
	printf("%d\n",max(0,max(min(len[0]-s,len[1]),min(len[0],s+len[1]-g))));
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll; ll g, h;

int main()
{
    ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );

    struct days
    {
        ll l, r, t, w;

        days()
        {
            cin >> l >> r >> t, w = r - l + 1;

            if ( g > 0 )
                g = gcd( g, t );
            else
                g = t;
        }

        void max_width( const days& b ) const
        {
            ll d = ( b.l - l ) % g;

            if ( d < 0 )
                d += g;

            h = max( h, min( w, b.w + d ) - d );
        }

    } a[ 2 ];

    for( int i = 0, j = 1; i < 2; i++, j-- )
        a[ i ].max_width( a[ j ] );

    cout << h;
}

#include <bits/stdc++.h>

using namespace std;

long long res (long long al, long long ar, long long bl, long long br) {
    return (min(ar, br) - max(al, bl) + 1);
}

int main() {
    long long al, ar, ta; cin >> al >> ar >> ta;
    long long bl, br, tb; cin >> bl >> br >> tb;

    long long g = __gcd(ta, tb);

    long long ga = al - (al % g);
    al -= ga;
    ar -= ga;

    long long gb = bl - (bl % g);
    bl -= gb;
    br -= gb;

    long long result = res(al, ar, bl, br);

    result = max(result, res(al + g, ar + g, bl, br));

    result = max(result, res(al - g, ar - g, bl, br));

    ga = ar - (ar % g);
    ar -= ga;
    al -= ga;

    gb = br - (br % g);
    br -= gb;
    bl -= gb;

    result = max(result, res(al, ar, bl, br));

    result = max(result, res(al + g, ar + g, bl, br));

    result = max(result, res(al - g, ar - g, bl, br));

    cout << (result > 0 ? result : 0) << endl;

    return 0;
}

#include <iostream>
#include <stdio.h>
#include <cmath>

#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

ll get(ll l1, ll r1, ll l2, ll r2)
{
    return max(0ll, min(r1, r2) - max(l1, l2) + 1);
}

ll gcd(ll a, ll b)
{
    if (!b)
        return a;
    return gcd(b, a % b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    ll i,n,l1,l2,r1,r2,t1,t2,k,ans = 0,d;
    cin >> l1 >> r1 >> t1 >> l2 >> r2 >> t2;

    d = gcd(t1, t2);
    if (d == 1)
    {
        cout << min(r1 - l1, r2 - l2) + 1;
        return 0;
    }

    if (t1 > t2)
    {
        swap(l1, l2);
        swap(r1, r2);
        swap(t1, t2);
    }
    if (l1 > l2)
    {
        l2 += t2;
        r2 += t2;
        t2 *= 2;
    }
    ll cnt;
    for (i = (l2 - l1) / d * d, cnt = 0; i <= t2 && cnt < 10; i += d, cnt++)
    {
        if (r1 + i < t2 || l1 + i >= t2)
            ans = max(ans, get((l1 + i) % t2, (r1 + i) % t2, l2, r2));
        else
        {
            ans = max(ans, get(l1 + i, t2 - 1, l2, r2));
            ans = max(ans, get(0, (r1 + i) % t2, l2, r2));
        }
    }
    cout << ans;
    return 0;
}

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long la,ra,ta,lb,rb,tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;

	long long g = __gcd(ta, tb);
	long long res = 0;
	for (int a = 0; a < 4; a++)
		for (int b = 0; b < 4; b++) {
			long long sta = (la % g) + a * g;
			long long stb = (lb % g) + b * g;
			long long eda = sta + ra - la + 1;
			long long edb = stb + rb - lb + 1;
			res = max(res, min(eda, edb) - max(sta, stb));
		}
	cout << res << endl;

    return 0;
}

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using matrix = vector<vector<long long>>;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pd;

template<typename T>
using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
//#define int ll

void FAST_IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.setf(ios::fixed);
    //cout.precision(20);
    #ifdef _offline
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // _offline
}

signed main() {
    FAST_IO();
    ll la, ra, ta;
    ll lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    if (la > lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }
    ll g = __gcd(ta, tb);
    ll f = (lb - la) % g;
    ll s = ta - g + f;
    ll ans = max(0LL, min(ra - la + 1 - f, rb - lb + 1));
    ans = max(ans, min(ra - la + 1, rb - lb + 1 - ta + s));
    cout << ans;
}

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(1e9+7)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
ll l,r,t,l1,r1,t1,ans,k,qw;
ll as(ll a,ll b,ll c,ll d) {return max(0ll,min(b,d)-max(a,c)+1);}
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>l>>r>>t>>l1>>r1>>t1;
    if(t1>t) swap(t1,t),swap(l,l1),swap(r,r1);
    k=__gcd(t,t1);qw=l1-l;qw%=k;
    ans=max(as(l,r,l+qw,l+qw+r1-l1),as(l,r,l+k+qw,l+k+qw+r1-l1));
    ans=max(ans,as(l,r,l+qw-k,l+qw-k+r1-l1));
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define key pair<ll,ll>
#define iii pair<ii,int>
#define vii vector<pair<ll,ll> >
#define vi vector<int>
#define vvi vector<vector<ll> >
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define nu 100001
#define mod 998244353
#define inf pow(10,14)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;

    ll La = ra-la+1;
    ll Lb = rb-lb+1;

    if(ta>tb)
    {
        ll temp = Lb;
        Lb = La;
        La = temp;

        temp = tb;
        tb = ta;
        ta = temp;

        temp = lb;
        lb = la;
        la = temp;

        temp = rb;
        rb = ra;
        ra = temp;
    }

    ll rem = ((la-lb)%tb+tb)%tb;

    ll Diff = gcd(ta,tb);
    ll buffy = rem/Diff;

    ll lowpoint = rem-buffy*Diff;

    ll Val = min(Lb-1,lowpoint+La-1)-lowpoint+1;

    ll highpoint = (((tb)-rem)/Diff)*Diff+rem;

    ll diffr = tb-highpoint;

    ll Val2 = min(La,diffr+Lb)-diffr;

    cout<<max((ll)0,max(Val2,Val));
}

#import<cstdio>
long long G(long long x,long long y)
{
	return y?G(y,x%y):x;
}
long long a,b,f,g,k,la,lb,ra,rb,ta,tb;
main()
{
	scanf("%lld%lld%lld%lld%lld%lld",&la,&ra,&ta,&lb,&rb,&tb);
	if(la>lb)g=la,la=lb,lb=g,g=ra,ra=rb,rb=g,g=ta,ta=tb,tb=g;
	f=G(ta,tb),g=ta*tb/f;
	k=(lb-la)/f*f,la+=k,ra+=k;
	a=(ra<rb?ra:rb)-lb+1;
	la+=f,ra+=f;
	b=(ra<rb?ra:rb)-la+1;
	printf("%lld",(a>b?a:b)>0?a>b?a:b:0);
}

//By Tushar Gautam
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define memreset(a) memset(a,0,sizeof(a))
#define testcase(t) int t;cin>>t;while(t--)
#define forstl(i,v) for(auto &i: v)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define leadzero(a) __builtin_clz(a) // count leading zeroes
#define trailzero(a) __builtin_ctz(a) // count trailing zeroes
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln "\n"
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define dbg2(x,y) cerr<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<endl;
#define dbgstl32(v) cerr<<#v<<" = \n"; { int c=0; forstl(it,v) \
cerr<<"    Term "<< ++c <<" = "<<it<<ln;} cerr<<endl
#define dbgstlp32(v) cerr<<#v<<" = \n"; { int c=0; forstl(it,v) \
cerr<<"    Term "<< ++c <<" = "<<it.fi<<" , "<<it.se<<ln;} cerr<<endl
#define dbgarr(v,s,e) cerr<<#v<<" = "; forsn(i,s,e) cerr<<v[i]<<", "; cerr<<endl
#define inp freopen("input.txt", "r", stdin)
#define outp freopen("output.txt", "w", stdout)
#define dbgm(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}
typedef long long int  ll;
typedef long double  ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
const int LIM = 1e5+5, MOD = 1e9+7;
int t,n,m,k,x,y;
int main(){
	fastio;
	ll la,ra,lb,rb;
	ll ta,tb;
	cin>>la>>ra>>ta>>lb>>rb>>tb;
	ll g=__gcd(ta,tb);
	ll l1= ra-la+1, l2 = rb-lb+1;
	if(l2>l1){
		swap(la,lb);swap(ra,rb);swap(ta,tb);swap(l1,l2);
	}
	ll diff=lb-la;
	ll k1=(diff/g)*g;
	ll ans=0;
	if(la==0 && ra==ta-1) ra=MOD;
	if(lb==0 && rb==tb-1) rb=MOD;
	//dbgm(diff,k1,g);
	forsn(i,-5,5){
		ans=max(ans,1+min(ra,rb-k1-i*g)-max(la,lb-k1-i*g));
	}
	forsn(i,-5,5){
		ans=max(ans,1+min(ra+k1+i*g,rb)-max(la+k1+i*g,lb));
	}
	cout<<ans<<ln;
	return 0;
}

/*
 ID: bradyawn
 PROG: contest
 LANG: C++11
 */

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <deque>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>
#include <utility>
#include <set>
#include <unordered_set>
#include <ctime>
#include <queue>
#include <stack>
#include <bitset>
#include <random>
#include <cstring>
#include <complex>
#include <cassert>

#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> i2;
typedef pair<ll,ll> ll2;

ll ret = 0;

ll La, Ra, Ta;
ll Lb, Rb, Tb;

ll GCD = 0;

ll2 gcd(ll a, ll b) //ax + by = GCD
{
    if (b == 0) {GCD = a; return {1,0};}
    ll2 nxt = gcd(b, a%b);
    ll x = nxt.first;
    ll y = nxt.second;
    ll k = a/b;
    return {y, x-k*y};
}

int main()
{
    //ifstream inf("");
    //ofstream outf("");
    cout << setprecision(10);
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> La >> Ra >> Ta; //ll Sa = La+Ta-Ra;
    cin >> Lb >> Rb >> Tb; //ll Sb = Lb+Tb-Rb;

    ll2 eq = gcd(Ta, Tb); //ax+by = gcd

    ll L0 = La%GCD, R0 = L0 + (Ra-La);
    ll L1 = Lb%GCD, R1 = L1 + (Rb-Lb);

    La = L0;
    Ra = R0;
    Lb = L1;
    Rb = R1;

    for (ll offA = -1; offA <= 1; offA++)
        for (ll offB = -1; offB <= 1; offB++)
        {
            ll N_Ra = Ra + offA*Ta;
            ll N_La = La + offA*Ta;
            ll N_Rb = Rb + offB*Tb;
            ll N_Lb = Lb + offB*Tb;
            ret = max(ret, min(N_Ra,N_Rb)-max(N_La,N_Lb)+1);
        }

    cout << ret << '\n';

    return 0;

}

/*
 Read the problem carefully.
 Don't forget to sort(), mod, ll!!!!
 Initial value = +/- infinity instead of zero!!!
 */

#include<bits/stdc++.h>

using namespace std;

#define int long long

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int la, ra, ta;
    int lb, rb, tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int g = __gcd(ta, tb);
    if (abs(la - lb) % g == 0) {
        cout << min(ra - la + 1, rb - lb + 1);
        return 0;
    }
    int ans = 0;
    int p = (g - ((la - lb) % g + g) % g) % g;
    ans = max(ans, min(ra - la + 1 - p, rb - lb + 1));
    int p2 = (g - ((lb - la) % g + g) % g) % g;
    ans = max(ans, min(rb - lb + 1 - p2, ra - la + 1));
    cout << ans;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef double db;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pct __builtin_popcount

int ff(int la, int ra, int ta, int lb, int rb, int tb) {
	if (la >= ta) {
		return ff(la-ta, ra-ta, ta, lb, rb, tb);
	}
	if (ra >= ta) {
		return max(ff(la, ta-1, ta, lb, rb, tb), ff(0, ra-ta, ta, lb, rb, tb));
	}
	int rs = min(ra, rb);
	int ls = max(la, lb);
	if (rs < ls) return 0;
	return rs-ls+1;
}

int main() {
	int la, ra, ta, lb, rb, tb;
	cin >> la >> ra >> ta >> lb >> rb >> tb;
	if (ta < tb) {
		swap(la, lb);
		swap(ra, rb);
		swap(ta, tb);
	}
	int g = __gcd(ta, tb);
	int aa = la/g;
	la -= aa*g;
	ra -= aa*g;
	int bb = lb/g;
	lb -= bb*g;
	rb -= bb*g;
	int S = 0;
	S = max(S, ff(la, ra, ta, lb, rb, tb));
	S = max(S, ff(la+g, ra+g, ta, lb, rb, tb));
	S = max(S, ff(la+ta-g, ra+ta-g, ta, lb, rb, tb));
	cout << S << endl;
	return 0;
}

#include <iostream>
#include <iomanip>
#include <set>
#include <algorithm>
#include <unordered_map>
#include <assert.h>
#include <cstring>
#include <vector>
#include <chrono>
#include <random>
#include <cmath>
#include <queue>
#include <functional>
#include <map>

using namespace std;
#define N 100005
#define M 2503
#define MOD 1000000007
#define LL long long int
#define INF 1e18

int par[N],sub[N],comp;
LL v[N];
int root(int u) {
  while(par[u]!=u) {
    par[u]=par[par[u]];
    u=par[u];
  }
  return u;
}
void merge(int uu,int vv) {
  int u=root(uu),v=root(vv);
  if (u==v) return;
  if (sub[u]<sub[v]) swap(u,v);
  sub[u]+=sub[v];
  par[v]=u;
  comp--;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
  LL la,ra,ta,lb,rb,tb;
  cin >> la >> ra >> ta >> lb >> rb >> tb;
  if (la>lb) {/*
    LL g=__gcd(ta,tb);
    LL st=la+(g-(la-lb)%g)%g,en;
    en=min(ra,rb);
    cout << en-st+1 << '\n';*/
    swap(la,lb);
    swap(ra,rb);
    swap(ta,tb);
  }
  if (lb>la) {
    LL g=__gcd(ta,tb);
    LL st=la+(lb-la)%g,en,ans;
    en=min(ra,st+rb-lb);
    ans=en-st+1;
    st=lb+(g-(lb-la)%g)%g;
    en=min(rb,st+ra-la);
    ans=max(ans,en-st+1);
    ans=max(ans,0*1LL);
    cout << ans << '\n';

  }
  else {
    cout << min(ra,rb)-la+1 << '\n';
  }
}

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long la,ra,ta,lb,rb,tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;

	long long g = __gcd(ta, tb);
	long long res = 0;
	for (int a = 0; a <= 5; a++)
		for (int b = 0; b <= 5; b++) {
			long long sta = (la % g) + a * g;
			long long stb = (lb % g) + b * g;
			long long eda = sta + ra - la + 1;
			long long edb = stb + rb - lb + 1;
			res = max(res, min(eda, edb) - max(sta, stb));
		}
	cout << res << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll a, b, ta, c, d, tb;
	cin >> a >> b >> ta >> c >> d >> tb;
	if (c < a) {
		swap(a, c);
		swap(b, d);
		swap(ta, tb);
	}
	ll g = __gcd(ta, tb);
	ll diff = (c - a) / g;
	a += g * diff;
	b += g * diff;
	ll ans = 0;
	for (int it = 0; it < 100; ++it) {
		ans = max(ans, min(b, d) - max(a, c) + 1);
		a += g;
		b += g;
	}
	cout << ans << endl;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll gcd(ll a,ll b)
{
    if(!b)
        return a;
    return gcd(b,a%b);
}
int main()
{
    int i,j,k;
    ll x1,x2,y1,y2,z1,z2,s,g,d,mmax,len1,len2;
    scanf("%I64d%I64d%I64d",&x1,&y1,&z1);
    scanf("%I64d%I64d%I64d",&x2,&y2,&z2);
    if(x1>x2)
    {
        s=x1;
        x1=x2;
        x2=s;
        s=y1;
        y1=y2;
        y2=s;
        s=z1;
        z1=z2;
        z2=s;
    }
    len1=y1-x1+1;
    len2=y2-x2+1;
    g=gcd(z1,z2);
    d=x2-x1;
    d=d%g;
    mmax=0;
    mmax=max(mmax,min(len2,len1-d));
    d=g-d;
    mmax=max(mmax,min(len1,len2-d));
    printf("%I64d\n",mmax);
    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <math.h>
#include <stack>
#include <deque>
#include <numeric>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <functional>
#define mpair make_pair
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const ld epsylon = 1e-9;

const std::string PROGRAM_NAME = "ivo";

long long evkl(long long a, long long b) {
  long long r;
  if (b > a)
    swap(b, a);
  if (a == 0) {
    if (b != 0)
      return b;
    else
      return 1;
  }
  if (b == 0)
    return a;
  while (a % b) {
    r = a % b;
    a = b;
    b = r;
  }
  return b;
}

int main() {
  // freopen((PROGRAM_NAME + ".in").c_str(), "r", stdin);
  // freopen((PROGRAM_NAME + ".out").c_str(), "w", stdout);

  ll la, ra, ta;
  ll lb, rb, tb;
  cin >> la >> ra >> ta;
  cin >> lb >> rb >> tb;

  ll d = evkl(ta, tb);

  if (d == 1) {
    cout << min(ra -la + 1, rb - lb + 1) << endl;
    return 0;
  }
  ll u = (lb%d - la%d + d) % d;
  ll v = (la%d - lb%d + d) % d;
  ll res1 = min(ra -la + 1, rb - lb + 1 - v);

  ll res2 = min(rb -lb + 1, ra - la + 1 - u);

  ll ans = max(res1, res2);
  ans = max(ans, 0LL);
  cout << ans << endl;
  return 0;
}

#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int MOD=1e9+7;
LL f(LL l1,LL r1,LL l2,LL r2)
{
	return max(0LL,min(r1,r2)-max(l1,l2)+1);
}
int main()
{
	LL la,ra,ta,lb,rb,tb;
	while(cin>>la>>ra>>ta>>lb>>rb>>tb)
	{
		la++,ra++,lb++,rb++;
		LL d=__gcd(ta,tb);
		LL ans=0;
		if(rb==ra)ans=f(la,ra,lb,rb);
		else if(ra<rb)
		{
			LL t=(rb-ra)/d;
			la+=t*d;
			ra+=t*d;
			//不一定刚好对齐，所以在不多出一点和多出一点选最大
			ans=f(la,ra,lb,rb);
			ans=max(ans,f(la+d,ra+d,lb,rb));
		}
		else if(ra>rb)
		{
			LL t=(ra-rb)/d;
			lb+=t*d;
			rb+=t*d;
			ans=f(la,ra,lb,rb);
			ans=max(ans,f(la,ra,lb+d,rb+d));
		}
		cout<<ans<<endl;
	}
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
#define int long long
#define pii pair<int,int>
#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
#define fr first
#define se second
#define mp make_pair
const int mod=(int)1e9+7;
const int inf=(int)1e18;

using namespace std;
inline int binexp(int a,int b,int m)
{
    int res=1;
    a%=m;
    while(b)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}
struct interval
{
	int l,r,t;
	interval()
	{
		l=r=t=0;
	}
}intr[2];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r",stdin);
    freopen("out.txt", "w", stdout);
    #endif
    for(int i=0;i<2;i++)
    {
    	cin>>intr[i].l>>intr[i].r>>intr[i].t;
    }
    int ans=0;
    if(intr[0].l != 0 && intr[1].l  != 0)
    {
    	int m=min(intr[0].l,intr[1].l);
    	for(int i=0;i<2;i++)
    		intr[i].l-=m,intr[i].r-=m;
    }
    if(intr[0].l>intr[1].l)
    	swap(intr[0],intr[1]);
    if(intr[1].l<=intr[0].r)
    	ans=min(intr[0].r,intr[1].r)-intr[1].l+1;
  //  cout<<ans<<"\n";
    if(intr[0].t!=intr[1].t && (intr[0].l!=0 || intr[1].l!=0))
    {
    	if(intr[0].t>intr[1].t)
    		swap(intr[0],intr[1]);
    	int d=__gcd(intr[0].t,intr[1].t);
    	//cout<<d<<"\n";
    	int k=abs(intr[0].l-intr[1].l),ind;
    	if(k%d==0)
    	{
    		ans=min(intr[0].r-intr[0].l,intr[1].r-intr[1].l)+1;
    	}
    	else
    	{
    		if(intr[0].l!=0)
    			swap(intr[0],intr[1]);
    		ind=(k/d)*d;
    		//cout<<ind<<" "<<k<<" "<<d<<"\n";
    		if(intr[1].l<=ind || (intr[0].r>=intr[1].l-ind))
    			ans=max(ans,min(intr[1].r-max(intr[1].l,ind),intr[0].r-intr[0].l-(intr[1].l-ind))+1);
    		ind+=d;
    		if(intr[1].l<=ind &&  intr[1].r>=ind)
    			ans=max(ans,min(intr[1].r-max(intr[1].l,ind),intr[0].r-intr[0].l)+1);
    	}
    	/*int ind=((k/d+(k%d==0?0:1))*d+intr[0].l)%intr[1].t;
    	cout<<ind<<"\n";
    	if(intr[1].l<=ind && ind<=intr[1].r)
    		ans=max(ans,min(intr[1].r-ind,intr[0].r-intr[0].l)+1);
    	else if(intr[1].l>ind)
    		ans=max(ans,min(intr[1].r-intr[1].l+1,min(0ll,1+intr[0].r-intr[0].l-(intr[1].l-ind))));
    	ind=(-(k/d)*d+intr[1].l)%intr[0].t;
    	//cout<<ind<<"\n";
    	if(intr[0].l<=ind && ind<=intr[0].r)
    		ans=max(ans,min(intr[1].r-intr[1].l,intr[0].r-ind)+1);
    	else if(intr[0].l>ind)
    		ans=max(ans,min(intr[0].r-intr[0].l+1,min(0ll,intr[1].r-intr[1].l-(intr[0].l-ind)+1)));*/
    }
    cout<<ans<<"\n";
 	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll la,ra,ta,lb,rb,tb;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>la>>ra>>ta>>lb>>rb>>tb;

	if(la>lb)
	{
		swap(la,lb);
		swap(ra,rb);
		swap(ta,tb);
	}
	ll tmp=__gcd(ta,tb);

	/*if((lb-la)%tmp==0)
	{
		cout<<max(-lb+ra+1,0ll)<<endl;
	}*/
	/*else
	{*/

	ll ans=0,move=((lb-la)/tmp)*tmp;
	la += move;
	ra+=move;
	// (ra-la+1)-(lb-la) ,  (
	ans=min(rb-lb+1,ra-lb+1);
	ans=max(0ll,ans);
	la+=tmp;
	ra+=tmp;
	ll ans1=min(rb-la+1,ra-la+1);
	ans=max(ans,ans1);
	cout<<ans<<endl;
	/*}*/
	return 0;
}

// A.cpp: 定义控制台应用程序的入口点。
//

#include <algorithm>
#include <iostream>
using namespace std;

int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    return b == 0 ? a : gcd(b, a%b);
}

int solve(int la, int lena, int lb, int lenb)
{
    int ra = la + lena - 1;
    int rb = lb + lenb - 1;
    return max(0, min(ra, rb) - max(la, lb)+1);
}

int main()
{
    int la, ra, ta;
    int lb, rb, tb;

    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;

    int g = gcd(ta, tb);
    int lena = ra - la + 1;
    int lenb = rb - lb + 1;
    int diff = ra - rb;

    int ans = 0;

    ans = max(ans, solve(la%g, lena, lb%g, lenb));
    ans = max(ans, solve(la%g+g, lena, lb%g, lenb));
    ans = max(ans, solve(la%g, lena, lb%g+g, lenb));

    cout << ans << endl;

    return 0;
}

#include <iostream>

using namespace std;

long long la,ra,ta,lb,rb,tb,r;

long long getInter() {
    if (ra < lb) return 0;
    if (rb < la) return 0;
    return min(ra,rb) - max(la, lb) + 1;
}

long long gcd (long long a, long long b) {
    if (a%b == 0) return b;
    return gcd (b, a%b);
}

int main()
{
    r = 0;
    cin >> la >> ra >>  ta >> lb >> rb >> tb;
    long long g = gcd(ta, tb);
    if ((ra - la) < (rb - lb)) {
        swap(ra, rb);
        swap(la, lb);
    }
    long long dif = la - lb;
    dif = dif / g * g;
    lb += dif;
    rb += dif;
    long long cr = getInter();
    r = max (r, cr);
    lb -= 3*g;
    rb -= 3*g;
    for (long long i=-2; i<2; i++) {
        lb += g;
        rb += g;
        cr = getInter();
        r = max(r, cr);
    }
    dif = ra - rb;
    dif = dif / g * g;
    lb += dif + g;
    rb += dif + g;
    for (long long i=1; i>-2; i--) {
        cr = getInter();
        r = max(r, cr);
        lb -= g;
        rb -= g;
    }
    cout << r;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct S
{
    ll l, r, t;
}a, b;

ll g, ans;

int main()
{
    ios_base::sync_with_stdio(false);

    cin>>a.l>>a.r>>a.t>>b.l>>b.r>>b.t;
    g=__gcd(a.t, b.t);

    if(b.l-a.l%g<0) swap(a, b);

    ll length=a.r-a.l;
    a.l%=g;
    a.r=a.l+length;

    ll x=(b.l-a.l)/g*g+a.l;
    if(x+length>=b.l) ans=min(b.r, x+length)-b.l+1;

    //cout<<x<<" "<<ans<<endl;
    x+=g;

    if(x>b.r);
    else if(x+length<=b.r) ans=max(ans, length+1);
    else ans=max(ans, b.r-x+1);

    cout<<ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define INFll 0x3f3f3f3f3f3f3f3f
#define GCD(a,b) __gcd(a,b)
#define ll long long

struct Date
{
    ll l,r,t;
    ll len(){return r-l+1;};
}a,b;

ll F(ll L1,ll R1,ll L2,ll R2)
{
    return max(min(R1,R2)-max(L1,L2)+1,1ll*0);
}
ll Solve()
{
    ll gcd=GCD(a.t,b.t);
    ll c=abs(a.l-b.l);
    if(c%gcd == 0)
        return min(a.len(),b.len());
    ll d1=(b.l-a.l)%gcd;
    if(d1 < 0)
        d1 += gcd;
    ll d2=gcd-d1;
    ll ans=max(F(0,a.r-a.l,d1,d1+b.r-b.l),F(0,b.r-b.l,d2,d2+a.r-a.l));
    return ans;
}
int main()
{
//    freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
    scanf("%lld%lld%lld",&a.l,&a.r,&a.t);
    scanf("%lld%lld%lld",&b.l,&b.r,&b.t);
//    printf("%lld,%lld,%lld\n",a.l,a.r,a.t);
//    printf("%lld,%lld,%lld\n",b.l,b.r,b.t);
    printf("%lld\n",Solve());

    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long la, ra, ta;
    long long lb, rb, tb;

    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;

    if(ra - la < rb - lb)
    {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }

    long long k = __gcd(ta, tb);

    ra -= la;
    lb -= la;
    rb -= la;
    la = 0;

    long long pos = lb % tb;
    if(pos < 0)
        pos += tb;

   // cout << pos << endl;

    pos %= k;

   // cout << pos << ", " << k << endl;

    long long sol1 = min(rb - lb + 1, ra - pos + 1);
    long long sol2 = max(0LL, pos - k + (rb - lb) - la + 1);

   // cout << sol1 << " " << sol2 << endl;

    long long sol = max(sol1, sol2);

    cout << sol << endl;

    return 0;
}

#include <iostream>

using namespace std;

long long la,ra,ta,lb,rb,tb,r;

long long getInter() {
    if (ra < lb) return 0;
    if (rb < la) return 0;
    return min(ra,rb) - max(la, lb) + 1;
}

long long gcd (long long a, long long b) {
    if (a%b == 0) return b;
    return gcd (b, a%b);
}

int main()
{
    r = 0;
    cin >> la >> ra >>  ta >> lb >> rb >> tb;
    long long g = gcd(ta, tb);
    if ((ra - la) < (rb - lb)) {
        swap(ra, rb);
        swap(la, lb);
    }
    long long dif = la - lb;
    dif = dif / g * g;
    lb += dif;
    rb += dif;
    long long cr = getInter();
    r = max (r, cr);
    lb -= 3*g;
    rb -= 3*g;
    for (long long i=-2; i<1000; i++) {
        lb += g;
        rb += g;
        cr = getInter();
        r = max(r, cr);
    }
    dif = ra - rb;
    dif = dif / g * g;
    lb += dif + g;
    rb += dif + g;
    for (long long i=1; i>-1000; i--) {
        cr = getInter();
        r = max(r, cr);
        lb -= g;
        rb -= g;
    }
    cout << r;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

static long long l1, l2, r1, r2, t1, t2, g;

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

inline long long f(long long t)
{
    return max(0LL, min(r1, r2 + t * g) - max(l1, l2 + t * g) + 1);
}

int main()
{
    scanf("%lld%lld%lld%lld%lld%lld", &l1, &r1, &t1, &l2, &r2, &t2);
    g = gcd(t1, t2);
    long long dl = l1 - l2, dr = r1 - r2;
    if (dl < dr)
        swap(l1, l2), swap(r1, r2), dl = -dl, dr = -dr;
    long long tl = ceil(1.0 * dl / g), tr = floor(1.0 * dr / g);
//    printf("%lld %lld\n", tl, tr);
    if (tl <= tr)
        return printf("%lld\n", min(r1 - l1 + 1, r2 - l2 + 1)), 0;
    printf("%lld\n", max(max(f(tl - 1), f(tl)), max(f(tr + 1), f(tr))));
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const int INF = 1e9 + 5;

typedef long long ll;

ll la, ra, ta, lb, rb, tb;
ll ans;

int main() {
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    ll t = __gcd(ta, tb);
    ll l1 = la % t,
    l2 = lb % t;
    ll r1 = l1 + (ra - la),
    r2 = l2 + (rb - lb);
    for (ll i = -2; i <= 2; i++) {
        for (ll j = -2; j <= 2; j++) {
            la = l1 + ta * i;
            ra = r1 + ta * i;
            lb = l2 + tb * j;
            rb = r2 + tb * j;
            ans = max(ans, min(ra, rb) - max(la, lb) + 1);
        }
    }
    cout << ans;
    return 0;
}

#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>

using namespace std;

typedef long long ll;

const int lim = 1e8;

int my_solve(int la, int ra, int ta, int lb, int rb, int tb) {
    int dt = abs(ta - tb);
    if (dt == 0) {
        return max(0, min(ra, rb) - max(la, lb) + 1);
    }
    int k = (rb - ra) / gcd(dt, tb);
    int rr = (ra + k * gcd(dt, tb)) % tb;
    int ll = -ra + la + rr;
    int res = min(rb, rr) - max(lb, ll) + 1;
    return max(res, 0);
}

int ok_solve(int la, int ra, int ta, int lb, int rb, int tb) {
    if (gcd(ta, tb) == 1) {
        return min(rb - lb + 1, ra - la + 1);
    } else {
        int ans = 0;
        if (ra > rb) {
            swap(ra, rb);
            swap(la, lb);
            swap(ta, tb);
        }
        int dt = abs(ta - tb);
        for (int i = 0; i < lim; i++) {
            la += dt;
            ra += dt;
            if (ra > la) {
                int ll = max(la, lb);
                int rr = min(ra, rb);
                ans = max(rr - ll + 1, ans);
            }
        }
        return ans;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    #ifdef LOCAL
    assert(freopen("input.txt", "r", stdin));
    assert(freopen("output.txt", "w", stdout));
    #endif

    int la, lb, ra, rb, ta, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    int f = my_solve(la, ra, ta, lb, rb, tb);
    int s = my_solve(lb, rb, tb, la, ra, ta);
    cout << max(f, s);
}

#include <cstdio>

int n, m, l;

struct Section
{
	long long l, r, t;
	long long len;
};

Section a, b;

long long gcd(long long a, long long b)
{
	if (a % b == 0)
		return b;
	else
		return gcd(b, a % b);
}

long long getAns(Section &a, Section &b, long long gT)	//a.len > b.len
{
	long long maxLength = 0;
	long long dl = a.l - b.l;
	long long k;
	if (dl < 0)
		k = dl / gT - 1;
	else
		k = dl / gT - 1;

//	long long curLengthA, curLengthB;
	long long lastLength = -1;
	long long curLength = 1;
	long long nbl = 0, nbr;
//	while (curLength > 0)
	while (nbl <= a.r)
	{
		nbl = b.l + k * gT;
		nbr = b.r + k * gT;

//		if (a.r > nbr) {
		if (a.l > nbl) {
			if (a.r > nbr)
				curLength = nbr - a.l + 1;	//rb - la;
			else
				curLength = a.r - a.l + 1;	//rb - la;
		} else {
			if (a.r > nbr)
				curLength = nbr - nbl + 1;
			else
				curLength = a.r - nbl + 1;
		}

		if (curLength < 0)
			curLength = 0;

		if (maxLength < curLength)
			maxLength = curLength;

		if (maxLength >= b.len)
			return maxLength;

		if ((lastLength == -1) || (lastLength <= curLength)) {
			k++;
			lastLength = curLength;
		} else {
			break;
		}
	}

//	long long dr = b.r - a.r;
//	if (dr < 0)
//
//
//	lastLength = -1;

	return maxLength;
}

int main()
{
	scanf("%I64d%I64d%I64d", &(a.l), &(a.r), &(a.t));
	a.len = a.r - a.l + 1;
	scanf("%I64d%I64d%I64d", &(b.l), &(b.r), &(b.t));
	b.len = b.r - b.l + 1;

	long long gT;
	if (a.t > b.t)
		gT = gcd(a.t, b.t);
	else
		gT = gcd(b.t, a.t);

	if (a.t == b.t)
	{
		long long dist;

		if (a.l <= b.l)
		{
			if (a.r > b.r)
				dist = b.r - b.l + 1;
			else
				dist = a.r - b.l + 1;
		}
		else
		{
			if (a.r > b.r)
				dist = b.r - a.l + 1;
			else
				dist = a.r - a.l + 1;
		}

		if (dist < 0)
			dist = 0;

		printf("%I64d\n", dist);

		return 0;
	}

	if (gT == 1)
	{
		if (a.len > b.len)
			printf("%I64d\n", b.len);
		else
			printf("%I64d\n", a.len);
	}

	long long ans;
	if (a.len >= b.len)
		ans = getAns(a, b, gT);
	else
		ans = getAns(b, a, gT);

	printf("%I64d\n", ans);

	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; ///find_by_order(),order_of_key()

int main()
{
    int la,ra,ta;
    scanf("%i %i %i",&la,&ra,&ta);
    int lb,rb,tb;
    scanf("%i %i %i",&lb,&rb,&tb);
    int diff=abs(ta-tb);
    if(diff==0)
    {
        int resl=max(la,lb);
        int res2=min(ra,rb);
        printf("%i\n",max(res2-resl+1,0));
        return 0;
    }
    int deltal=la-lb;
    int deltar=ra-rb;
    if((ll)deltal*deltar<=0)
    {
        int a=max(abs(la-ra+1),abs(lb-rb+1));
        printf("%i\n",a);
        return 0;
    }
    if(deltal<0)
    {
        deltal*=-1;
        deltar*=-1;
        //printf("%i %i\n",deltal,deltar);
        int mog=deltal/diff;
        deltal-=mog*diff;
        deltar-=mog*diff;
        int poz1=la+deltal;
        int poz2=ra+deltar;
        int r1=max(0,min(poz2,ra)-max(la,poz1)+1);
        poz1-=diff;
        poz2-=diff;
        r1=max(r1,min(poz2,ra)-max(la,poz1)+1);
        printf("%i\n",r1);
        return 0;
    }
    if(deltal>=0)
    {
        int mog=deltar/diff;
        deltal-=mog*diff;
        deltar-=mog*diff;
        int poz1=la-deltal;
        int poz2=ra-deltar;
        int r1=max(0,min(poz2,ra)-max(la,poz1)+1);
        poz1+=diff;
        poz2+=diff;
        r1=max(r1,min(poz2,ra)-max(la,poz1)+1);
        printf("%i\n",r1);
        return 0;
    }
    return 0;
}

/*siddharth goyal*/
#include<bits/stdc++.h>
using namespace std;
#define   pb              push_back
#define   REP(i,n)        for(i=1;i<=n;i++)
#define   FOR(i,a,b)      for(i=a;i<=b;i++)
#define   all(v)          v.begin(),v.end()
#define   F               first
#define   S               second
#define   vl              vector<LL>
#define   itr             ::iterator it
#define   lb              lower_bound
#define   ub              upper_bound
#define   LL              long long
#define   ULL             unsigned long long
LL n,i,j,ans = 0;
int main()
{   // Read the constraints,highlights,Time Limit,Test Cases
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    LL a,b,c ;
    LL d,e,f ;
    cin>>a>>b>>c ;
    cin>>d>>e>>f ;
    if(a==d)
      return cout<<min(b-a+1,e-d+1),0 ;
    LL gcd = __gcd(c,f) ;
    if(abs(d-a)%gcd==0)
    return cout<<min(b-a+1,e-d+1),0 ;
    else
    {  LL k = abs(d-a)/gcd ;
       k = (k + 1)*gcd  - abs(d-a) ;
       LL g = 0 ;
       g = d + k ;
       //cout<<"k = "<<k<<endl;
       if(g>e)  g = k%max((e-d),1LL) + d + 1 ;
       //cout<<"gg = "<<g<<endl;
       pair <LL,LL> p1 = {1,g} ;
       g = a + k ;
       if(g>b) g = k%max((b-a),1LL) + a + 1 ;
       pair <LL,LL> p2 = {1,g} ;
       if(p1.S<=e)
       {
          if(abs(a-p1.S)%gcd==0)
            ans = max(ans,min(b-a+1,e-p1.S+1)) ;
        }
       if(p2.S<=b)
       {   //cout<<p2.F<<endl ;
           if(abs(d-p2.S)%gcd==0)
             ans = max(ans,min(e-d+1,b-p2.S+1)) ;
       }

    }

    cout<<ans<<endl ;
}
//Check for 0,1 and other corner cases.

#include <bits/stdc++.h>
typedef long long ll;
#define repVector(v)  for( auto it = v.begin(); it != v.last(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define OUTFILE freopen("output.out","w",stdout)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll join(ll l1, ll r1, ll l2, ll r2) {
	ll l = max(l1, l2);
	ll r = min(r1, r2);
	return r - l + 1;
}

int main() {
	ll la, ra, ta;
	cin >> la >> ra >> ta;

	ll lb, rb, tb;
	cin >> lb >> rb >> tb;

	if(la > lb) {
		swap(la, lb);
		swap(ra, rb);
		swap(ta, tb);
	}

	ll mind = (lb - la);
	ll rangea = (ra - la);
	ll rangeb = (rb - lb);

	ll ans = 0;

	ll lefta = 0, righta = lefta + rangea;
	ll leftb = mind, rightb = leftb + rangeb;

	ans = max(ans, join(lefta, righta, leftb, rightb));

	lefta = ta, righta = lefta + rangea;
	ans = max(ans, join(lefta, righta, leftb, rightb));

	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n,m,l,a[100300],b[100300],pa[100300],vis[100300];
int parent(int x)
{
    if(pa[x] == x)
        return x;
    return pa[x] = parent(pa[x]);
}
int ans;
void del(int x)
{
    ans-= 1;
}
void ins(int x)
{
    ans+=1;
}
int main() {
#ifdef __jafar
    freopen("input.txt", "r", stdin);
#else
    //   freopen("jenga.in","r",stdin);
     //  freopen("jenga.out","a",stdout);
#endif
    int la,ra,ta,lb,rb,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;
    int g = __gcd(tb,ta);
    int ans = 0;
    int cnt =0;
    for(int i=2+(ra-rb)/g;;i--,cnt++)
    {
        ans = max(ans,min(ra,rb+i*g)-max(la,lb+i*g)+1);
        if(rb+i*g<lb||cnt>1e7)
            break;
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int la, ra, ta, lb, rb, tb;
int main() {
    scanf("%d%d%d%d%d%d", &la, &ra, &ta, &lb, &rb, &tb);
    int A = ra - la + 1, B = rb - lb + 1, G = __gcd(ta, tb), D = lb - la, ans = 0;
    D %= G, D = min(D, G - D);
    int l = D, r = D + B - 1;
    ans = max(ans, A + r - l + 1 - max(r, A-1) + min(l, 0) - 1);
    printf("%d", ans);
}

#include <bits/stdc++.h>
using namespace std;
long long a, b, t, x,y,T,aa,as,k,ans,bb,g;
long long f(long long xx)
{
    if(xx>=0) return xx;
    else return 0;
}
int main()
{
    cin>>a>>b>>t;
    cin>>x>>y>>T;
    as=b-a;
    if(__gcd(t,T)==1)
    {
        cout<<min(y-x+1,b-a+1);
        return 0;
    }
    if(t>T)
    {
        swap(a,x);
        swap(b,y);
        swap(t,T);
    }
    a%=(__gcd(t,T));
    b=a+as;

    if(__gcd(t,T)>=2)
    {
        g=__gcd(t,T);
        long long ii;
        ii=(x-aa)/g;
        for(long long i=ii-10;i<=ii+10;i++)
        {
            aa=a+i*g;
            bb=aa+as;
            ans=max(ans,f(min(bb,y)-max(aa,x)+1)+f(min(bb,y+T)-max(aa,x+T)+1));
        }
        cout<<ans;
        return 0;
    }
    cout<<ans;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

typedef long long int ll;
typedef long double lf;

#define pll pair<ll,ll>
#define vll vector<ll>
#define vpl vector<pair<ll,ll>>
#define ppll pair<ll,pair<ll,ll>>
#define pb  push_back

#define ordered_set tree< ll , null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update >

using namespace __gnu_pbds;
using namespace std;

ll mod = 1e9+7;

ll power(ll x, ll y)
{
    ll res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res;
}

int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	ll la,ra,ta;
	ll lb,rb,tb;

	ll ans = 0 , k;

	cin>>la>>ra>>ta;
	cin>>lb>>rb>>tb;

	int cnt = 100000;

	if(lb < la){
	        swap(la,lb);
    	    swap(ra,rb);
    	    swap(ta,tb);
	}

	k = 0;

	if(lb >= ra){
	    k = ceil((lf)(lb-ra)/(lf)ta);
	}

	ra += k*ta; la += k*ta;

	while(cnt--){
	    ll lower_val = max(la , lb);
	    ll upper_val = min(ra , rb );

	   if(upper_val>=lower_val){
	       ans = max( ans , abs(upper_val - lower_val + 1) );
	   }else{
	       break;
	   }

	   ra += ta; la += ta;
	}

	cout<<ans;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;

int Ex_line(int l,int r,int L,int R) {
	int t1 = max(l,L);
	int t2 = min(r,R);
	return max(t2 - t1,0);
}
int main() {
	int l,r,t;
	int L,R,T;
	scanf("%d %d %d",&l,&r,&t);
	scanf("%d %d %d",&L,&R,&T);
	int d = __gcd(t,T);
	int len1 = r - l + 1;
	int len2 = R - L + 1;
	l %= d,L %= d;
	r = l + len1;
	R = L + len2;
	int ans = Ex_line(l,r,L,R);
	ans = max(ans,Ex_line(l + t, r + t, L , R));
	ans = max(ans,Ex_line(l , r, L + T, R + T));
	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;

int la, ra, ta, lb, rb, tb;

int main() {
	scanf("%d%d%d%d%d%d", &la, &ra, &ta, &lb, &rb, &tb);
	if( ta == tb) {
		int L = max(la, lb), R = min(ra, rb);
		printf("%d\n", max(0, R-L+1)); return 0;
	}
	int gc = __gcd(ta, tb);
	if( gc == 1) return printf("%d\n", min(ra-la,rb-lb)+1), 0;
	if( ta > tb) swap(la, lb), swap(ra, rb), swap(ta, tb);
	int k = (tb - la - 1) / ta + 1;
	int L = (la + ta * k) % tb, R = (ra + ta * k) % tb;
	if( L >= lb) return printf("%d\n", max(0, min(rb, R) - L + 1)), 0;
	k = (lb - la) / ta;
	int ans = 0;
	L = L + k * ta; R = R + k * ta; ans = max(ans, min(R, rb) - max(L, lb) + 1);
	L += ta; R += ta; ans = max(ans, min(R, rb) - max(L, lb) + 1);
	printf("%d\n", ans);
	return 0;
}

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
typedef long long Long;
typedef double Double;

template <class U, class V>
istream& operator>>(istream& is, pair<U, V>& p) {
  is >> p.first >> p.second;
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& x : v) {
    is >> x;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& x : v) {
    os << x << " ";
  }
  return os;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  Long la, ra, ta;
  Long lb, rb, tb;

  cin >> la >> ra >> ta;
  cin >> lb >> rb >> tb;
  Long res = 0;
  for (int i = 0; i < 2; ++i) {
    Long g = __gcd(ta, tb);
    Long d1 = ((max(0LL, lb - la) + g - 1)) / g * g;
    res = max(res, rb - d1);
    swap(la, lb);
    swap(ra, rb);
    swap(ta, tb);
  }

  cout << res << endl;

  return 0;
}

/*
 ID: bradyawn
 PROG: contest
 LANG: C++11
 */

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <deque>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>
#include <utility>
#include <set>
#include <unordered_set>
#include <ctime>
#include <queue>
#include <stack>
#include <bitset>
#include <random>
#include <cstring>
#include <complex>
#include <cassert>

#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> i2;
typedef pair<ll,ll> ll2;

ll La, Ra, Ta;
ll Lb, Rb, Tb;

int main()
{
    //ifstream inf("");
    //ofstream outf("");
    cout << setprecision(10);
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> La >> Ra >> Ta; //ll Sa = La+Ta-Ra;
    cin >> Lb >> Rb >> Tb; //ll Sb = Lb+Tb-Rb;

    if (Ra < Lb)
    {
        ll K = ceil(1.0*(Lb-Ra)/Ta);
        La += Ta*K;
        Ra += Ta*K;
    }
    if (Rb < La)
    {
        ll K = ceil(1.0*(La-Rb)/Tb);
        Lb += Tb*K;
        Rb += Tb*K;
    }

    //Ra >= Lb
    //Rb >= La

    //cout << La << ' ' << Ra << endl;
    //cout << Lb << ' ' << Rb << endl;

    ll ret = max(0LL, min(Ra,Rb)-max(La,Lb)+1);
    cout << ret << '\n';

    return 0;

}

/*
 Read the problem carefully.
 Don't forget to sort(), mod, ll!!!!
 Initial value = +/- infinity instead of zero!!!
 */

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
#define int long long
#define pii pair<int,int>
#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
#define fr first
#define se second
#define mp make_pair
const int mod=(int)1e9+7;
const int inf=(int)1e18;

using namespace std;
inline int binexp(int a,int b,int m)
{
    int res=1;
    a%=m;
    while(b)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}
struct interval
{
	int l,r,t;
	interval()
	{
		l=r=t=0;
	}
}intr[2];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r",stdin);
    freopen("out.txt", "w", stdout);
    #endif
    for(int i=0;i<2;i++)
    {
    	cin>>intr[i].l>>intr[i].r>>intr[i].t;
    }
    int ans=0;
    if(intr[0].l>intr[1].l)
    	swap(intr[0],intr[1]);
    if(intr[1].l<=intr[0].r)
    	ans=min(intr[0].r,intr[1].r)-intr[1].l+1;
    if(intr[0].t!=intr[1].t && (intr[0].l!=0 || intr[1].l!=0))
    {
    	if(intr[0].t>intr[1].t)
    		swap(intr[0],intr[1]);
    	int d=intr[1].t-intr[0].t;
    	int k=abs(intr[0].l-intr[1].l);
    	int ind=((k/d+(k%d==0?0:1))*d+intr[0].l)%intr[1].t;
    //	cout<<ind<<"\n";
    	if(intr[1].l<=ind && ind<=intr[1].r)
    		ans=max(ans,min(intr[1].r-ind,intr[0].r-intr[0].l)+1);
    	ind=((k/d+(k%d==0?0:1))*d+intr[1].l)%intr[0].t;
    //	cout<<ind<<"\n";
    	if(intr[0].l<=ind && ind<=intr[0].r)
    		ans=max(ans,min(intr[1].r-intr[1].l,intr[0].r-ind)+1);
    }
    cout<<ans<<"\n";
 	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ff first
#define ss second
const char nl = '\n';
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */
#include <bits/extc++.h>
namespace pbds =  __gnu_pbds;
template <typename key, typename val=pbds::null_type, typename comp=less<key> >
using ostree = pbds::tree<key, val, comp, pbds::rb_tree_tag,
                          pbds::tree_order_statistics_node_update>;
using vi = vector< int >; using vll = vector< ll >;
#define allOf(x) std::begin(x), std::end(x)
template <typename...Args>
ostream&operator<<(ostream&os, pair<Args...> const &p)
{return os<<'('<<p.first<<", "<<p.second<<')';}
template <typename...Args>
ostream&operator<<(ostream&os, vector<Args...> const &v)
{os<<'[';for(const auto&el:v)os<<' '<<el;return(os<<" ]");}
template <typename...Args> ostream&operator<<(ostream&os, map<Args...>const&v)
{os<<'[';for(auto const&el:v)os<<' '<<el;return(os<<" ]");}
template <typename...Args> ostream&operator<<(ostream&os, set<Args...>const&v)
{os<<'[';for(auto const&el:v)os<<' '<<el;return(os<<" ]");}
template <typename...Args>
ostream&operator<<(ostream&os, unordered_map<Args...>const&v)
{os<<'[';for(auto const&el:v)os<<' '<<el;return(os<<" ]");}
template<typename T> void trace_rec(stringstream&sst, const T&t)
{ string arg; getline(sst, arg, ','); cerr << arg << " : " << t << " }"; }
template<typename T, typename... Args>
void trace_rec(stringstream&sst, const T&t, const Args... args)
{ string arg; getline(sst, arg, ','); cerr << arg << " : " << t << ", ";
  trace_rec(sst, args...); }
template<typename... Args> void trace(const string&s, Args... args)
{stringstream sst(s); cerr << "{ "; trace_rec(sst, args...); cerr << endl;}
#define dbg(...) trace(#__VA_ARGS__, __VA_ARGS__)
template <typename T> void dbr(const T lb, const T ub)
{cerr<<'{';for(auto it=lb; it!=ub; it++)cerr<<' '<<(*it);cerr<<" }"<<endl;}
template<typename T, typename Comp=less<T> >
bool setMin(T &mem, const T&v, Comp comp = Comp())
{ return comp(v, mem) ? mem = v, true : false; }
template<typename T, typename Comp=less<T> >
bool setMax(T &mem, const T&v, Comp comp = Comp())
{ return comp(mem, v) ? mem = v, true : false; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */

ll solve(ll la,ll ra,ll ta,ll lb,ll rb,ll tb) {
  ll g = __gcd(ta, tb);
  if( g == 1 ) { return min(ra-la+1, rb-lb+1); }

  ll dist = ta; // tb / g;

  ll shift = (lb-la+dist-1)/dist;

  ll lx = la + shift * dist;
  ll rx = lx + (ra - la);
  dbg(dist, shift, la, ra, lx, rx, lb, rb);

  auto cut = [=](const ll &lx, const ll &rx, const ll&lb, const ll &rb)
  { return max(0ll, min(rx, rb)-max(lx, lb)+1); };

  ll ans = 0;

  for(ll k=-10; k<=10; k++) {
    ll rlx = cut(lx+k*dist, rx+k*dist, lb, rb);
    // dbg(ans, dist, shift, la, ra, lx, rx, lb, rb);
    setMax(ans, rlx);
  }

  return ans;
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  ll la, ra, ta, lb, rb, tb;
  cin >> la >> ra >> ta >> lb >> rb >> tb;

  ll ans = solve(la,ra,ta,lb,rb,tb);
  setMax(ans, solve(lb,rb,tb,la,ra,ta));
  cout << ans << endl;

  return 0;
}

/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;

/************************************/

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<double> VD;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<string> VS;

template <typename T>
using us = unordered_set<T>;

template <typename K, typename V>
using um = unordered_map<K, V>;

/************************************/

const int MOD = 1e9 + 7;
const double EPS = 1e-6;

/************************************/

int gcd(int x, int y) {
    return x ? gcd(y % x, x) : y;
}

int lcm(int x, int y) {
    return x * y / gcd(x, y);
}

int powerMOD(int x, int y) {
    if (!y) return 1;
    LL p = powerMOD(x, y / 2) % MOD;
    p = (p * p) % MOD;
    return y % 2 ? (x * p) % MOD : p;
}

int modInverse(int x) {
    return powerMOD(x, MOD - 2);
}

int binCoef(int n, int k) {
    LL ans = n - k + 1;
    for (int i = 2; i <= k; i++) {
        ans = ans * (n - k + i) % MOD;
        ans = ans * modInverse(i) % MOD;
    }
    return ans;
}

bool isPrime(LL x) {
    if (x % 2 == 0) return x == 2;
    for (LL i = 3; i <= sqrt(x); i += 2) {
        if (x % i == 0) return false;
    }
    return true;
}

/************************************/

template <typename F, typename S>
string to_string(pair<F, S> p) {
    return to_string(p.first) + ',' + to_string(p.second);
}

template <typename T>
string to_string(vector<T>& v) {
    ostringstream oss;
    auto out = ostream_iterator<T>(oss, ", ");
    if (!v.empty()) {
        copy(v.begin(), v.end() - 1, out);
        oss << v.back();
    }
    return oss.str();
}

/************************************/

template <typename F, typename S>
ostream& operator<<(ostream& os, const pair<F, S>& p) {
    return os << '(' << p.first << ", " << p.second << ')';
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '[';
    typename vector<T>::const_iterator it;
    for (it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) os << ", ";
        os << *it;
    }
    return os << ']';
}

template <typename T>
ostream& operator<<(ostream& os, const us<T>& v) {
    os << '{';
    typename us<T>::const_iterator it;
    for (it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) os << ", ";
        os << *it;
    }
    return os << '}';
}

template <typename K, typename V>
ostream& operator<<(ostream& os, const um<K, V>& m) {
    os << '{';
    typename um<K, V>::const_iterator it;
    for (it = m.begin(); it != m.end(); it++) {
        if (it != m.begin()) os << ", ";
        os << it->first << "-->" << it->second;
    }
    return os << '}';
}

#define deb(x) cerr << #x << " = " << x << endl;

/************************************/

int main() {
    ios::sync_with_stdio(false);
    int la, ra, ta, lb, rb, tb, ans = 0;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int g = gcd(ta, tb);
    int diff = (lb - ra) / g * g;
    la += diff;
    ra += diff;
    ans = max(ans, min(ra, rb) - max(la, lb) + 1);
    diff = ((lb - la - 1) / g + 1) * g;
    la += diff;
    ra += diff;
    ans = max(ans, min(ra, rb) - max(la, lb) + 1);
    cout << ans << endl;
    return 0;
}

//teja349
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;

#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define flush fflush(stdout)
#define primeDEN 727999983
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// find_by_order()  // order_of_key
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define int ll

int getdist(int a,int b,int c,int d){
	if(b<=d){
		return max(b-c+1,(ll)0);
	}
	return d-c+1;
}

signed main(){
    std::ios::sync_with_stdio(false); cin.tie(NULL);
    int la,ra,ta;
    int lb,rb,tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;
    int haha;
    int gg=__gcd(ta,tb);
    haha=(la-lb)%gg;
    haha+=gg;
    haha%=gg;
   // cout<<haha<<endl;
    int val=getdist(lb,rb,lb+haha,ra-la+lb+haha);
    haha=gg-haha;
    haha%=gg;
    int val1=getdist(lb,rb,lb+haha,ra-la+lb+haha);
    cout<<max(val1,val);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define key pair<ll,ll>
#define iii pair<ii,int>
#define vii vector<pair<ll,ll> >
#define vi vector<int>
#define vvi vector<vector<ll> >
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define nu 100001
#define mod 998244353
#define inf pow(10,14)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;

    ll La = ra-la+1;
    ll Lb = rb-lb+1;

    if(ta>tb)
    {
        ll temp = Lb;
        Lb = La;
        La = temp;

        temp = tb;
        tb = ta;
        ta = temp;

        temp = lb;
        lb = la;
        la = temp;

        temp = rb;
        rb = ra;
        ra = temp;
    }

    if(la<lb)
    {
        la+=ta;
    }

    ll rem = la-lb;

    ll Diff = gcd(ta,tb);

    ll lowpoint = rem;

    ll Val = min(Lb-1,rem+La-1)-lowpoint+1;

    ll highpoint = (((tb)-rem)/Diff)*Diff+rem;

    ll diffr = tb-highpoint;

    ll Val2 = La-diffr;

    cout<<max((ll)0,max(Val2,Val));

}

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define ll long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

main()
{
    int la, ra, ta, lb, rb, tb; cin>>la>>ra>>ta>>lb>>rb>>tb;
    if(la > lb)
    {
        swap(la, lb); swap(ra, rb); swap(ta, tb);
    }
    int xx = (lb - la) % ta;
    int x = __gcd(xx, tb);
    int y = __gcd(x, ta);
    int x1 = tb / y, y1 = ta /y;
    if(__gcd(x1, y1) == 1)
    {
        cout<<min(ra - la + 1, rb - lb + 1); return 0;
    }
    int t = min(xx, ta - xx );
    int ans = min(ra - la + 1 - t, rb - lb + 1);
    cout<<max(0ll, ans);

}

///*
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#define ll long long
#define ld long double

using namespace std;

ll gcd(ll a, ll b) {
	ll help;
	while (b > 0) {
		help = b;
		b = a % b;
		a = help;
	}
	return a;
}

int main() {
	ll l1, r1, t1;
	ll l2, r2, t2;
	cin >> l1 >> r1 >> t1;
	cin >> l2 >> r2 >> t2;
	if (t1 > t2) {
		swap(t1, t2);
		swap(l1, l2);
		swap(r1, r2);
	}
	ll x = gcd(t1, t2);
	ll before = (l2 / x) * x;
	ll R = before + (r1 - l1);
	ll ans = 0;
	ll ans1;
	ans1 = R - l2 + 1;
	ans = max(ans, ans1);
	before += x;
	ll L = before;
	R = before + (r1 - l1);
	if (R > r2) R = r2;
	ans1 = R - L + 1;
	ans = max(ans, ans1);
	cout << ans << endl;
}
//*/

#include <bits/stdc++.h>

#define int ll
#define fir first
#define sec second

typedef long long ll;
typedef long double ld;

using namespace std;

int get(int l1, int r1, int l2, int r2) {
    return(max(ll(0), min(r1, r2) - max(l1, l2) + 1));
}

signed main() {
    int la, ra, ta, lb, rb, tb;

    cin >> la >> ra >> ta >> lb >> rb >> tb;

    int ans = get(la, ra, lb, rb);

    int c = __gcd(ta, tb);

    if (la > lb) {
        swap(la, lb);
        swap(ra, rb);
    }
    int len = rb - lb;
    la = 0;
    ra -= la;
    lb -= la;
    rb -= la;

    lb %= c;

    ans = max(ans, get(la, ra, lb, lb + len));
    ans = max(ans, get(la, ra, -lb, -lb + len));

    lb -= c;

    ans = max(ans, get(la, ra, lb, lb + len));
    ans = max(ans, get(la, ra, -lb, -lb + len));

    cout << ans << '\n';
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

typedef long long int ll;
typedef long double lf;

#define pll pair<ll,ll>
#define vll vector<ll>
#define vpl vector<pair<ll,ll>>
#define ppll pair<ll,pair<ll,ll>>
#define pb  push_back

#define ordered_set tree< ll , null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update >

using namespace __gnu_pbds;
using namespace std;

ll mod = 1e9+7;

ll power(ll x, ll y)
{
    ll res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res;
}

int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	ll la,ra,ta;
	ll lb,rb,tb;

	ll ans = 0 , k;

	cin>>la>>ra>>ta;
	cin>>lb>>rb>>tb;

	int cnt = 10000000;

	if(lb < la){
	        swap(la,lb);
    	    swap(ra,rb);
    	    swap(ta,tb);
	}

	k = 0;

	if(lb >= ra){
	    k = ceil((lf)(lb-ra)/(lf)ta);
	}

	ra += k*ta; la += k*ta;

	int flag = 0;

	while(cnt--){

	    //cout<<la<<" "<<ra<<" "<<lb<<" "<<rb<<endl;

	    ll lower_val = max(la , lb);
	    ll upper_val = min(ra , rb );

	   if(upper_val>=lower_val){
	       ans = max( ans , abs(upper_val - lower_val )+1 );
	   }

	   if(lb < la){
	        swap(la,lb);
    	    swap(ra,rb);
    	    swap(ta,tb);
	    }

	    k = 1;

    	if(lb >= ra){
    	    k = ceil((lf)(lb-ra)/(lf)ta);
    	}

    	ra += k*ta; la += k*ta;

    }

	cout<<ans;

}

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define lo long long
#define inf 1000000000
#define md 1000000007
#define pb push_back
#define li 100005
using namespace std;
lo int la,ra,ta,lb,rb,tb,mx;
lo int kes(lo int a,lo int b,lo int c,lo int d){
	if(max(a,c)>min(b,d)) return 0;
	return min(b,d)-max(a,c)+1;
}
int main(){
	scanf("%lld %lld %lld",&la,&ra,&ta);
	scanf("%lld %lld %lld",&lb,&rb,&tb);
	lo int eb=__gcd(ta,tb);
	lo int xa=la%eb;
	lo int ya=ra-la+xa;
	lo int xb=lb%eb;
	lo int yb=rb-lb+xb;
	if(ya<=xa) ya+=eb;
	if(yb<=xb) yb+=eb;
	mx=max(mx,kes(xa,ya,xb,yb));
	mx=max(mx,kes(xa+eb,ya+eb,xb,yb));
	mx=max(mx,kes(xa,ya,xb+eb,yb+eb));
	lo int kisa=min(ra-la+1,rb-lb+1);
	lo int uzun=max(ra-la+1,rb-lb+1);
	if(kisa>=eb || uzun>=eb)
		printf("%lld\n",kisa);
	else{
		printf("%lld\n",mx);
	}
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define int long long
using namespace std;

int a,b,c,x,y,z,ans=1e18;

signed  main(){
    ios_base::sync_with_stdio(0);  cin.tie(); cout.tie();
    cin >> a >> b >> c >> x >> y >> z;
    if(a==x){
        cout<<min(b,y)-a+1;
        return 0;
    }
    double l1=a;
    double l2=x;
    double cr=__gcd(c,z);
    double k=-(l2-l1)/cr;
    int a1=floor(k);
    int a2=ceil(k);
    int diff1=x-a+a1*cr;
    int diff2=x-a+a2*cr;
    a1=max(a,a+diff1);
    a2=min(b,a+diff1+(y-x));
    if(a2-a1+1>0){
    ans=min(ans,a2-a1+1);}
    a1=max(a,a+diff2);
    a2=min(b,a+diff2+(y-x));
    if(a2-a1+1>0){
    ans=min(ans,a2-a1+1);}
    if(ans==1e9) rc(0);
    cout<<ans;

}

#include<bits/stdc++.h>
using namespace std;
#define INFll 0x3f3f3f3f3f3f3f3f
#define GCD(a,b) __gcd(a,b)
#define ll long long

struct Date
{
    ll l,r,t;
    ll len(){return r-l+1;};
}a,b;

ll F(ll L1,ll R1,ll L2,ll R2)
{
    return max(min(R1,R2)-max(L1,L2)+1,1ll*0);
}
ll Solve()
{
    ll gcd=GCD(a.t,b.t);
    ll c=abs(a.l-b.l);
    if(c%gcd == 0)
        return min(a.len(),b.len());
    ll d1=b.l-a.l;
    if(d1 < 0)
        d1 += gcd;
    ll d2=gcd-d1;
    ll ans=max(F(0,a.r-a.l,d1,d1+b.r-b.l),F(0,b.r-b.l,d2,d2+a.r-a.l));
    return ans;
}
int main()
{
//    freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
    scanf("%lld%lld%lld",&a.l,&a.r,&a.t);
    scanf("%lld%lld%lld",&b.l,&b.r,&b.t);
//    printf("%lld,%lld,%lld\n",a.l,a.r,a.t);
//    printf("%lld,%lld,%lld\n",b.l,b.r,b.t);
    printf("%lld\n",Solve());

    return 0;
}

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

ll get(ll l1, ll r1, ll l2, ll r2)
{
    if (l1 <= l2 && r1 >= r2)
        return r2 - l2 + 1;
    if (l2 <= l1 && r2 >= r1)
        return r1 - l1 + 1;
    if (l1 > r2 || r1 < l2)
        return 0;
    if (r1 >= l2 && l1 < l2)
        return r1 - l2 + 1;
    return r2 - l1 + 1;
}

ll gcd(ll a, ll b)
{
    if (!b)
        return a;
    return gcd(b, a % b);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    ll i,n,l1,l2,r1,r2,t1,t2,k,ans = 0;
    cin >> l1 >> r1 >> t1 >> l2 >> r2 >> t2;

    if (gcd(t1, t2) == 1)
    {
        cout << min(r1 - l1, r2 - l2) + 1;
        return 0;
    }

    if (t1 > t2)
    {
        swap(l1, l2);
        swap(r1, r2);
        swap(t1, t2);
    }

    if (t2 % t1 == 0)
    {
        k = ceil((l2 - r1) * 1.0 / t1);
        cout << max(get(l1 + k * t1, r1 + k * t1, l2, r2), get(l1 + (k + 1) * t1, r1 + (k + 1) * t1, l2, r2));
        return 0;
    }

    for (i = 0; i < 6e7; i++)
    {
        ans = max(ans, get(l1, r1, l2, r2));
        if (l1 < l2)
        {
            l1 += t1;
            r1 += t1;
        }
        else
        {
            l2 += t2;
            r2 += t2;
        }
    }
    cout << ans;
    return 0;
}

// 20 - 2
// 3.000
// 6
// 1589
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define mod1 1187278649
#define mod2 1149736019
#define ioi cout<<"------------ioi------------\n"
#define all(x) (x).begin(),(x).end()

const ld PI=3.14159265358979323;
const int linf=1000111000111000111LL;
const int inf=1011111111;
const int N=100005;

int gcd(int a, int b);
int power(int x, int y, int p);
int modInverse(int n, int p);
int nCrModPFermat(int n, int r, int p);
void pri(int n);
void sieve_primes(int n);
void sieve(int n);
int copa(int n);

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int test=1;
    // cin>>test;
    while(test--){
        int l1,r1,t1;
        cin>>l1>>r1>>t1;
        int l2,r2,t2;
        cin>>l2>>r2>>t2;
        int s1=(r1-l1+1);
        int s2=(r2-l2+1);
        int seg=min(s1,s2);
        if(t2==t1){
            int le=max(l1,l2);
            int ri=min(r1,r2);
            cout<<max((int)0,ri-le+1);
        }
        else if(t1>t2){
            if(t1%t2==0){
                int d1=l1/t2;
                int f=d1*t2;
                int s=(d1+1)*t2;
                int ans1=min(s2-(l1-f),s1);
                int ans2=min(s2,s1-(s-l1));
                int ans=max(ans1,ans2);
                cout<<max((int)0,ans);
            }
            else{
                cout<<seg<<"\n";
            }
        }
        else if(t1<t2){
            if(t2%t1==0){
                int d1=l2/t1;
                int f=d1*t1;
                int s=(d1+1)*t1;
                int ans1=min(s1-(l2-f),s2);
                int ans2=min(s1,s2-(s-l2));
                int ans=max(ans1,ans2);
                cout<<max((int)0,ans);
            }
            else{
                cout<<seg<<"\n";
            }
        }
    }
    cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}

int gcd(int a, int b){
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

// int power(int x, int y, int p){
//     int res=1;
//     x=x%p;
//     while(y>0)
//     {
//         if (y & 1)
//             res=(res*x)%p;
//         y=y>>1;
//         x=(x*x)%p;
//     }
//     return res;
// }

// int modInverse(int n, int p)
// {
//     return power(n,p-2,p);
// }

// int nCrModPFermat(int n, int r, int p)
// {
//     if(r==0)
//         return 1;
//     return ((fac[n]*modInverse(fac[r],p))%p*modInverse(fac[n-r],p)%p)%p;
// }

// !!!---> IMP <----!!! Create a vector of primes before using this function...
// void pri(int n)
// {
//     for(int i=2;i<=sqrt(n);i++)
//     {
//         if(n%i==0)
//         {
//             while(n%i==0) n=n/i;
//             primes.pb(i);
//        }
//     }
//     if(n>=2) primes.pb(n);
// }

// Create spf array and bool primes array....
// void sieve_primes(int n)
// {
//     primes[0]=true;
//     primes[1]=true;
//     for(int i=2;i*i<=n;i++)
//     {
//         if(primes[i]==false)
//         {
//             for(int j=i*i;j<=n;j+=i)
//                 primes[j]=true;
//         }
//     }
// }

// void sieve(int n)
// {
//     spf[1]=1;
//     for(int i=2;i<=n;i++) spf[i]=i;
//     for(int i=4;i<=n;i+=2) spf[i]=2;
//     for(int i=3;i*i<=n;i++)
//     {
//         if(spf[i]==i)
//         {
//             for(int j=i*i;j<=n;j+=i)
//             {
//                 if(spf[j]==j) spf[j]=i;
//             }
//         }
//     }
// }

// int copa(int n)
// {
//     int result=n;
//     for(int i=2;i<=sqrt(n);i++)
//     {
//         if(n%i==0)
//         {
//             while(n%i==0)
//                 n/=i;
//             result-=result/i;
//         }
//     }
//     if(n>1)
//         result-=result/n;
//     return result;
// }

#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;

ll gcd(ll a,ll b) {
    if(a < b) return gcd(b, a);
    if(b == 0) return a;
    return gcd(b, a%b);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll la, ra, ta;
    ll lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    if(ra-la > rb-lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);

    }
    ra -= la;
    lb -= la;
    rb -= la;
    la = 0;
    ll d = gcd(ta, tb);
    ll md = lb;
    while(md < 0) md += ta;
    md = md%d;

    ll ans = 0;
    rb += md-lb;
    lb = md;
    ans += max(ra-md+1, ll(0));
    ans += max(((rb-ta)/ta)*(ra-la+1) , ll(0));
    if(rb >= ta) {
        ans += min(((rb-ta)%ta)+1, ra-la+1);
    }
    ll ans2 = 0;
    if(md != 0) {
        md -= d;
        rb += md-lb;
        lb = md;
        ans2 += min(ra+1, rb+1);
        ans2 += max(((rb-ta)/ta)*(ra-la+1) , ll(0));
        if(rb >= ta) {
            ans2 += min(((rb-ta)%ta)+1, ra-la+1);
        }
    }
    cout << max(ans, ans2) << endl;

}

    //
    //  Created by Ruslan Jankurazov.
    //  Copyright © 2018 Ruslan Jankurazov. All rights reserved.
    //

    #pragma GCC optimize("Ofast")

    #include "bits/stdc++.h"

    #define pb push_back
    #define mp make_pair
    #define ll long long
    #define ull unsigned long long
    #define f first
    #define s second
    #define uint unsigned int
    #define pii pair <int, int>
    #define pll pair <ll, ll>
    #define sqr(x) (x)*(x)

    using namespace std;

    int main() {
    #define FILE_INPUT 0
    #define FILE ""
    #ifndef DEBUG
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #if FILE_INPUT == 1
    #warning "FREOPEN is on"
        freopen(FILE".in", "r", stdin);
        freopen(FILE".out", "w", stdout);
    #endif
    #endif
        ll l, r, t;
        ll a, b, c;
        cin >> l >> r >> t;
        cin >> a >> b >> c;
        ll mx = 0;
        for (int i = 0; i <= 1e7; ++i) {
            ll k = i;
            ll d = (r + k*t - b)/c;
            mx = max(mx, min((b + d*c), (r + k*t)) - max(l + k*t, a + d*c));
            //mx = max(mx, (b + d*c) - max(l + k*t, a + d*c));
            k = i;
            d = (r + k*t - b + c - 1)/c;
            mx = max(mx, min((b + d*c), (r + k*t)) - max(l + k*t, a + d*c));
        }
        cout << mx + 1;
        return 0;
    }

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define fi first
#define se second
#define filename "ch."
using namespace std;
const int N=1e3+3;
LL La,Lb,Ra,Rb,Ta,Tb;
int main()
{
    cin >> La >> Ra >> Ta;
    cin >> Lb >> Rb >> Tb;
    if(La < Lb ) {
        swap(La,Lb); swap(Ra,Rb);
        swap(Ta,Tb);
    }
    LL res = 0;
    for(LL k=0;k<=5000000;k++)
    {
        LL tLb=Lb,tRb=Rb;
        LL m = (La-tLb) / Tb;
        tLb = tLb + 1ll* Tb*(m);
        tRb = tRb + 1ll* Tb*(m);
        res = max(res,(long long) min(Ra,tRb)-La+1 );
      //  cerr<< La<<" "<<Ra<<'\n';
     //   cerr<< tLb<<" "<<tRb<<'\n';cerr<<'\n';
       // cerr<< m<<" ";
        tLb = Lb + 1ll* Tb*(m+1);
        tRb = Rb + 1ll* Tb*(m+1);
      //  cerr<< La<<" "<<Ra<<'\n';
      //  cerr<< tLb<<" "<<tRb<<'\n';cerr<<'\n';
        res = max(res,(long long) min(Ra,tRb)-tLb+1 );
        La = La+Ta ; Ra = Ra+Ta ;
    }
    cout << res;
}

/* dsingh_24 */

#include<bits/stdc++.h>
#define ll          int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll,pair<ll,ll> >
#define vll          vector<ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000000
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

#define N  200008

int main()
{
    ios
    ll l1,r1,t1,l2,r2,t2;
    cin>>l1>>r1>>t1>>l2>>r2>>t2;
    ll dif=abs(l1-l2);
    ll gcd=__gcd(t1,t2),ans;
    if(l2>=l1){
	    dif%=gcd;
	    ans=max(r1-l1+1-dif,r2-l2+1-(gcd-dif)%gcd);
    }else{
	    dif%=gcd;
	    ans=max(r1-l1+1-(gcd-dif)%gcd,r2-l2+1-dif);
    }
    cout<<ans;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int > pii;
const int inf = 1e9;
#define ss second
#define ff first
ll la, ra, ta, lb, rb, tb;

main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int a0, a1, a2, b0, b1, b2;
    a0 = la, b0 = lb;
    int mini = min(la, lb);
    la -= mini, lb -= mini;
    a1 = ra - la+1, b1 = rb - lb + 1;
    a2 = ta - ra + la - 1;
    b2 = tb - rb + lb - 1;

    if(a0 > 0){
        swap(a0, b0);
        swap(a1, b1);
        swap(a2, b2);
    }
    int lala = __gcd(a1+a2, b1+b2);
   // cout << a0 << " " << a1 << " " << a2 << " " << lala << "\n";
   // cout << b0 << " " << b1 << " " << b2 << "\n";
    if(__gcd(a1+a2, b1+b2) == 0) cout << min(a1, b1);
    else{
        int poc = b0;
        int wyn = min(b1, a1-poc);
        wyn = max(wyn, min(a1, b1 + poc - lala));
        cout << max(wyn, 0LL);
    }
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    bool f=true;
    long long int la,ra,ta,lb,rb,tb,cnt=-1,k,l,r;
    cin>>la>>ra>>ta>>lb>>rb>>tb;
    while(f)
    {
        if(lb>ra)
        {
            k=(lb-la)/ta;
            la+=k*ta;
            ra+=k*ta;
            if(lb>ra)
            {
                la+=ta;
                ra+=ta;
            }
            if(la>rb) f=false;
            continue;
        }
        if(la>rb)
        {
            k=(la-lb)/tb;
            lb+=k*tb;
            rb+=k*tb;
            if(la>rb)
            {
                lb+=tb;
                rb+=tb;
            }
            if(lb>ra) f=false;
            continue;
        }
        l=la;
        if(lb>l) l=lb;
        r=ra;
        if(rb<r) r=rb;
        if(r-l+1==cnt) break;
        if(r-l+1>cnt) cnt=r-l+1;
    }
    if(cnt==-1) cnt=0;
    cout<<cnt<<'\n';
    return 0;
}

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include "ext/pb_ds/tree_policy.hpp"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/rope"

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define f first
#define s second

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
ll la, ra, ta, lb, rb, tb;
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    if(la > lb) swap(la, lb), swap(ta, tb), swap(ra, rb);
    ll g = __gcd(ta, tb);
    ra -= (la / g) * g; la -= (la / g) * g;
    rb -= (lb / g) * g; lb -= (lb / g) * g;
    cout << max(0LL, max(min(ra, rb) - max(la, lb) + 1, min(ra + g, rb) - max(la + g, lb) + 1));
    return 0;
}

#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <functional>
#include <queue>
#include <algorithm>
#include <map>
#include <set>

using namespace std;

typedef struct {
	int r;
	int c;
} Pos;

typedef struct {
	int l;
	int r;
} Interval;

typedef long long ll;

const int INF = 2147483647;
const double EPS = 0.0001;
const double PI = 3.1415926;
const int MOD = 998244353;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
	int la, ra, ta;
	int lb, rb, tb;
	scanf("%d %d %d", &la, &ra, &ta);
	scanf("%d %d %d", &lb, &rb, &tb);
	int n = __gcd(ta, tb);
	int ans = 0;
	int k = 0;
	int z = 0;
	for(int i = 0; i < n; i++){

		Interval invA = {la + k * ta, ra + k * ta};
		Interval invB = {lb + z * tb, rb + z * tb};
		int c = min(invB.r, invA.r) - max(invA.l, invB.l);
		if(c >= 0) {
			ans = max(ans, c + 1);
		}
		if(ta < tb){
			k++;
		}
		else if(ta == tb){
			break;
		}
		else{
			z++;
		}
	}
	printf("%d\n", ans);
	return 0;
}

// @formatter:off
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
#define forn(i,n) for(int i=0;i<(n);++i)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define sz(x) ((int)(x).size())
#define X first
#define Y second
template<class T> inline T sqr(T x) { return x * x; }
template<class T> inline T parse(const string&s){T x;stringstream ss(s);ss>>x;return x;}
// @formatter:on

ll common(ll l1, ll r1, ll l2, ll r2) {
    if (l1 > l2) {
        swap(l1, l2);
        swap(r1, r2);
    }
    ll r = min(r1, r2);
    if (r >= l2) return r - l2 + 1; else return 0;
}

ll gcd(ll a, ll b) {
    if (b) return gcd(b, a % b);
    return a;
}

int main() {
#ifdef EFGEN_DBG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ll l1, l2, r1, r2, t1, t2;
    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;
    if (t1 > t2) {
        swap(t1, t2);
        swap(l1, r2);
        swap(r1, r2);
    }
    if (t1 == t2) cout << common(l1, r1, l2, r2);
    else {
        ll res = 0;
        ll len = r1-l1;
        ll d = gcd(t1, t2);
        forn(i, t2 / d) {
            res = max(res, common(l1, l1+len, l2, r2));
            l1 = (l1 + d)%t2;
        }
        cout << res;

    }
    return 0;
}

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define clr(s, x) memset(s, x, sizeof(s))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
inline int read(){int r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
inline ll readll(){ll r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
inline ll qpow(ll a,ll b,ll mod){ll res=1;while(b){if(b&1)res = (res*a)%mod;a=(a*a)%mod;b>>=1;}return res;}
inline ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const double eps = 1e-8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 1e9+100;
const int MAXM = 1e5;
int main(){
	ll la,ra,ta;
	ll lb,rb,tb;
	cin>>la>>ra>>ta;
	cin>>lb>>rb>>tb;
	ll maxr=max(ra+ta-1,rb+tb-1);
	ll ans=0;
	for(int k=0;rb+k*tb<=(ra+ta-1)*(rb+tb-1);k++){
		ans=max(ans,min(ra,rb+k*tb)-max(la,lb+k*tb)+1);
	}
	for(int k=0;ra+k*ta<=(ra+ta-1)*(rb+tb-1);k++){
		ans=max(ans,min(ra+k*ta,rb)-max(la+k*ta,lb)+1);
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll cul(ll l1, ll r1, ll l2, ll r2, ll t) {
	l1 += t;
	r1 += t;
	return max(0ll, min(r1, r2) - max(l1, l2) + 1);
}

int main()
{
	ll l1, r1, v1, l2, r2, v2;
	while(~scanf("%lld %lld %lld %lld %lld %lld", &l1, &r1, &v1, &l2, &r2, &v2)) {
		l1++, r1++, l2++, r2++;
		ll d = __gcd(v1, v2);
		ll l = abs(r1 - r2);
		ll ans = 0;
		ll t1 = l / d * d;
		ll t2 = t1 + d;
		ans = max(ans, cul(l1, r1, l2, r2, t1));
		ans = max(ans, cul(l1, r1, l2, r2, t2));
		ans = max(ans, cul(l2, r2, l1, r1, t1));
		ans = max(ans, cul(l2, r2, l1, r1, t1));
		cout << ans << endl;
	}
	return 0;
}

#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define base 1000000000

using namespace std;
typedef long long ll;
typedef pair<int,int> ii;

int la,ra,ta,lb,rb,tb;

int ucln(int a,int b)
{
    if (b==0) return a;
    else return ucln(b,a%b);
}

int main()
{
    //freopen("text.INP","r",stdin);
   // freopen("text.OUT","w",stdout);

   cin>>la>>ra>>ta>>lb>>rb>>tb;
   if (la>=lb) swap(la,lb),swap(ra,rb),swap(ta,tb);
   if (ucln(ta,tb)==1)
   {
       cout<<min(ra-la,rb-lb);
   }
   else
   {
       int uc=ucln(ta,tb);
       int lanew=la+(lb-la)/uc*uc;
       int ranew=ra+(lb-la)/uc*uc;
       if (lb-lanew>lanew+uc-lb) lanew+=uc,ranew+=uc;
       int res=0;
       res=min(ranew,rb)-max(lanew,lb)+1;
       cout<<res;
   }

}

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
inline long long read () {
    char c = getchar();
    long long t = 0, f = 1;
    while (!isdigit(c)) f = (c == '-') ? -1 : 1, c = getchar();
    while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
    return t * f;
}
int a[N];
int b[N];
main()
{
    int n, m, i, j, t, d, l1, r1, t1, l2, t2, r2;
    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;
    int g = __gcd(t1, t2);
    int ans = 0;
    if(g == 1)  {
        cout << min(r1 - l1 + 1, r2 - l2 + 1);
        return 0;
    }
    else {
        int e = l2 - l1;
        if(e < 0) {
            e += t2;
        }
        e %= g;
        ans = max(ans, r1 - l1 + 1 - e);
        e = l1 - l2;
        if(e < 0) {
            e += t1;
        }
        ans = max(ans, r2 - l2 + 1 - e);
        //ans = min(e % g, ans);
    }
    cout << ans;
}

#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename Key=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<Key>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>class ordered_set_t:__gnu_pbds::tree<Key,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>{};const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>class hash_table_t:__gnu_pbds::gp_hash_table<KeyType,int,ghash>{};namespace vh{template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}inline void _W(const int&x){printf("%d",x);}inline void _W(const int64_t&x){printf("%" PRId64,x);}inline void _W(const double&x){printf("%.16f",x);}inline void _W(const char&x){putchar(x);}inline void _W(const char*x){printf("%s",x);}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}};
#define prec setprecision
namespace vh{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace vh{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;}}namespace vh{};
namespace vh{
  int main(){
    srand(GRANDOM);
    ll la,ra,ta;
    ll lb,rb,tb;
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;
    ll g=__gcd(ta,tb);
    if(((la-lb)%g)==0)cout<<min(ra-la+1,rb-lb+1)<<endl;
    else{
      ll x=(lb-la)/g*g;
      ll r=0;
      for(ll d=x-2;d<=x+2;d++){
        r=max(r,min(ra,rb+d*g)-max(la,lb+d*g)+1);
        r=max(r,min(ra+d*g,rb)-max(la+d*g,lb)+1);
      }
      cout<<r<<endl;
    }
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return vh::main();
}

#include <bits/stdc++.h>
using namespace std;
long long a, b, t, x,y,T,aa,as,k,ans,bb;
long long f(long long xx)
{
    if(xx>=0) return xx;
    else return 0;
}
int main()
{
    cin>>a>>b>>t;
    cin>>x>>y>>T;
    if(__gcd(t,T)==1)
    {
        cout<<min(y-x,b-x);
    }
    if(t>T)
    {
        swap(a,x);
        swap(b,y);
        swap(t,T);
    }
    as=b-a;
    a%=(__gcd(t,T));
    b=a+as;

    k=(x-a)/__gcd(t,T);
    if(x-a<0) k--;
    aa=a+__gcd(t,T)*k;
    bb=aa+as;
    ans=max(ans,f(min(bb,y)-max(aa,x)+1)+f(min(bb,y+T)-max(aa,x+T)+1)+f(min(bb,y+T+T)-max(aa,x+T+T)+1));
    k=(x-a)/__gcd(t,T);
    if(x-a>0) k++;
    aa=a+__gcd(t,T)*k;
    bb=aa+as;
    ans=max(ans,f(min(bb,y)-max(aa,x)+1)+f(min(bb,y+T)-max(aa,x+T)+1)+f(min(bb,y+T+T)-max(aa,x+T+T)+1));

    k=(y-a)/__gcd(t,T);
    if(y-a<0) k--;
    aa=a+__gcd(t,T)*k;
    bb=aa+as;
    ans=max(ans,f(min(bb,y)-max(aa,x)+1)+f(min(bb,y+T)-max(aa,x+T)+1)+f(min(bb,y+T+T)-max(aa,x+T+T)+1));
    k=(y-a)/__gcd(t,T);
    if(y-a>0) k++;
    aa=a+__gcd(t,T)*k;
    bb=aa+as;
    ans=max(ans,f(min(bb,y)-max(aa,x)+1)+f(min(bb,y+T)-max(aa,x+T)+1));

    cout<<ans;
}

#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <functional>
#include <queue>
#include <algorithm>
#include <map>
#include <set>

using namespace std;

typedef struct {
	int r;
	int c;
} Pos;

typedef struct {
	long long l;
	long long r;
} Interval;

typedef long long ll;

const int INF = 2147483647;
const double EPS = 0.0001;
const double PI = 3.1415926;
const int MOD = 998244353;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
	long long la, ra, ta;
	long long lb, rb, tb;
	scanf("%lld %lld %lld", &la, &ra, &ta);
	scanf("%lld %lld %lld", &lb, &rb, &tb);
	long long n = __gcd(ta, tb);
	long long ans = 0;
	int k = 0;
	int z = 0;
	for(int i = 0; i < n; i++){
		Interval invA = {la + k * ta, ra + k * ta};
		Interval invB = {lb + z * tb, rb + z * tb};
		long long c = min(invB.r, invA.r) - max(invA.l, invB.l);
		if(c >= 0) {
			ans = max(ans, c + 1);
		}
		if(ta < tb){
			k++;
		}
		else if(ta == tb){
			break;
		}
		else{
			z++;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fr ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include<algorithm>
#include<string>

ll la, ra, ta, lb, rb, tb;
int main()
{
	fr
	cin>>la>>ra>>ta>>lb>>rb>>tb;
	char arr1[1000], arr2[1000];
	memset(arr1, 0, 1000);
	memset(arr2, 0, 1000);
	for(ll i=0;i<=1000;i++)
	{
		if(i%ta>=la && i%ta<=ra)
			arr1[i]=1;
		if(i%tb>=lb && i%tb<=rb)
			arr2[i]=1;
	}
	ll count=0, max=0;
	for(ll i=0;i<=1000;i++)
	{
		if(arr1[i]==1 && arr2[i]==1)
			count++;
		else
		{
			if(count>max)	max=count;
			count=0;
		}
	}
	cout<<max<<endl;
	return 0;

}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const ll inf=1e18;
int main()
{
	ll la,ra,ta,lb,rb,tb;
	scanf("%lld %lld %lld",&la,&ra,&ta);
	scanf("%lld %lld %lld",&lb,&rb,&tb);
	if(ta<tb) swap(la,lb),swap(ra,rb),swap(ta,tb);
	lb-=la;
	rb-=la;
	ra-=la;
	la-=la;
	ll dif=rb-lb;
	tb=__gcd(tb,ta);
	lb=(lb%tb+tb)%tb;
	rb=lb+dif;
	ll test=2000000,sol=0;
	ll pla=la,plb=lb,pra=ra,prb=rb;
	while(test--)
	{
		sol=max(sol,min(ra,rb)-max(la,lb)+1);
		if(lb<la) lb+=tb,rb+=tb;
		else if(lb>la) la+=ta,ra+=ta;
		else lb+=tb,rb+=tb,la+=ta,ra+=ta;
		if(rb>inf || ra>inf) break;
	}
	test=2000000;
	la=pla;
	lb=plb;
	ra=pra;
	rb=prb;
	while(test--)
	{
		sol=max(sol,min(ra,rb)-max(la,lb)+1);
		if(lb<la) lb-=tb,rb-=tb;
		else if(lb>la) la-=ta,ra-=ta;
		else lb-=tb,rb-=tb,la-=ta,ra-=ta;
		if(lb<-inf || la<-inf) break;
	}
	printf("%lld\n",sol);
	return 0;
}

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma comment(linker, "/STACK:16777216")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using matrix = vector<vector<long long>>;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pd;

template<typename T>
using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
//#define int ll

void FAST_IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.setf(ios::fixed);
    //cout.precision(20);
    #ifdef _offline
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // _offline
}

signed main() {
    FAST_IO();
    ll l1, r1, t1;
    ll l2, r2, t2;
    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;
    if (l1 > l2) {
        swap(l1, l2);
        swap(r1, r2);
        swap(t1, t2);
    }
    ll plus1 = r1 - l1 + 1, minus1 = t1 - plus1;
    ll plus2 = r2 - l2 + 1, minus2 = t2 - plus2;
    if (plus1 == 0 || plus2 == 0) {
        cout << 0;
        return 0;
    }
    if (minus1 == 0) {
        cout << plus2;
        return 0;
    }
    if (minus2 == 0) {
        cout << plus1;
        return 0;
    }
    ll d = (l2 - l1) % t1;
    vector<pll> v;
    if (d == 0) {
        v.push_back({1, plus1});
        v.push_back({-1, minus1});
    }
    else if (d < plus1) {
        v.push_back({1, plus1 - d});
        v.push_back({-1, minus1});
        v.push_back({1, d});
    }
    else {
        v.push_back({-1, t1 - d});
        v.push_back({1, min(d, plus1)});
        if (d > plus1) {
            v.push_back({-1, d - plus1});
        }
    }
    ll g = __gcd(t1, t2);
    ll cur = 0;
    ll ans = 0;
    for (int i = 0; i < v.size(); ++i) {
        if (v[i].first == -1) {
            cur += v[i].second;
            continue;
        }
        ll lf = cur;
        ll rg = cur + v[i].second - 1;
        if (i + 1 == v.size() && v[0].first == 1) {
            rg += v[0].second;
        }
        ll start = lf + (lf % g);
        ans = max(ans, min(plus2, rg - start + 1));
        cur += v[i].second;
    }
    cout << ans;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a1,b1,t1,a2,b2,t2;
    cin>>a1>>b1>>t1>>a2>>b2>>t2;
    long long skok = __gcd(t1,t2);
    long long min1;
    long long min2;
    long long d1,d2,d;
    if(a2>a1)
    {
        d1=(a2-a1)%skok;
        d2=abs(d1-skok);
    }
    else
    {
        d1=(a1-a2)%skok;
        d2=abs(d1-skok);
    }
    //cout<<d1<<" "<<d2<<endl;
        min1 = max(0LL,min(b1-a1+1-d1,b2-a2+1));

        min2 = max(0LL,min(b2-a2+1-d2,b1-a1+1));
    cout<<max(min1,min2);
}

#pragma once
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define debug(x) std::cout << #x << ": " << x << '\n';
typedef long long ll;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll inter(ll a, ll b, ll a1, ll b1) {
	return std::max(0LL, std::min(b, b1) - std::max(a, a1) + 1);
}
ll less(ll a, ll b) {
	if (a < 0 && a % b != 0) {
		a += -b - (a % b);
	}
	return a / b;
}

int32_t main() {
	std::ios::sync_with_stdio(false);
	int l, r, t;
	int l1, r1, t1;
	std::cin >> l >> r >> t;
	std::cin >> l1 >> r1 >> t1;
	// ll razn = (t1 - t);
	// if (razn == 0) {
	// 	std::cout << inter(l, r, l1, r1);
	// 	return 0;
	// }
	ll answ = 0;
	ll minLen = std::min(r - l + 1, r1 - l1 + 1);
	ll f1 = -(r1 - l + 1 - minLen);
	if (f1 < 0 && f1 % gcd(t, t1) != 0) {
		f1 += -gcd(t, t1) -(f1 % gcd(t, t1));
	}
	ll answ1 = f1 / gcd(t, t1) * gcd(t, t1) + (r1 - l + 1);
	answ = std::max(answ, answ1);
	f1 = -(r - l1 + 1 - minLen);
	if (f1 < 0 && f1 % gcd(t, t1) != 0) {
		f1 += -gcd(t, t1) -(f1 % gcd(t, t1));
	}
	answ1 = f1 / gcd(t, t1) * gcd(t, t1) + (r - l1 + 1);
	ll d = gcd(t, t1);
	if (less(l1 - l, d) == less(r - r1, d)) {
		std::cout << minLen;
		return 0;
	}
	if (less(l - l1, d) == less(r1 - r, d)) {
		std::cout << minLen;
		return 0;
	}
	answ = std::max(answ, answ1);
	std::cout << answ;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long double LD;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef LOCAL
    #define DEBUG(x) { cerr << "# " << #x << ": " << x << endl;}
#else
    #define DEBUG(x)
#endif
mt19937_64 mt(time(0));
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/
LL gcd(LL a, LL b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

LL solve(LL a, LL b, LL x, LL y) {
    if (b < x || y < a) return 0;
    return min(b, y) - max(a, x) + 1;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cout.precision(10); cout << fixed;
#ifdef LOCAL
    freopen("../test.txt", "r", stdin);
    // freopen("../output.txt", "w", stdout);
#endif
    int a, b, c, x, y, z;
    cin >> a >> b >> c >> x >> y >> z;
    if (b-a+1 == c && y-x+1 == z) assert(false);
    if (b-a+1 == c) return y-x+1;
    if (y-x+1 == z) return b-a+1;
    LL tmp = gcd(c, z), delta, ret = 0;
    if (a > x) {
        delta = (a - x) % tmp;
        ret = solve(0, y-x, delta, delta+b-a);
        ret = max(ret, solve(z, z+y-x, delta, delta+b-a));
    } else {
        delta = (x - a) % tmp;
        ret = solve(0, b-a, delta, delta+y-x);
        ret = max(ret, solve(c, c+b-a, delta, delta+y-x));
    }
    cout << ret << endl;

/*---------------------------------------Coding Space End------------------------------------------*/
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}
/* author:  txingml */
/* picture is from hamayanhamayan(CF handle) */

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

#define FOR(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int la, ra, ta, lb, rb, tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int t = __gcd(ta, tb);
    int k1 = (lb - la) / t, k2 = k1 + 1;
    cout << max(min(ra + k1 * t, rb) - lb, min(ra + k2 * t, rb) - (la + k2 * t)) + 1 << endl;
}

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long la,ra,ta,lb,rb,tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;

	int g = __gcd(ta, tb);
	int res = 0;
	for (int a = 0; a <= 5; a++)
		for (int b = 0; b <= 5; b++) {
			int sta = (la % g) + a * g;
			int stb = (lb % g) + b * g;
			int eda = sta + ra - la + 1;
			int edb = stb + rb - lb + 1;
			res = max(res, min(eda, edb) - max(sta, stb));
		}
	cout << res << endl;

    return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fo(i,a,b) for(int i=a;i<b;i++)
#define fo_(i,a,b) for(int i=a;i>b;i--)
#define M(a) memset(a,0,sizeof a)
#define M_(a) memset(a ,-1,sizeof a)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265
#define nmax 100100
const ll inf = std::numeric_limits<ll>::max();
const ll mod = 1e9+7;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll md(ll a,ll d){
    return (a%d+d)%d;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll la,ra,ta;
    ll lb,rb,tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    ll tola,tolb;
    tola = ra - la+1;
    tolb = rb-lb+1;
    ll g = (ll)__gcd(ta,tb);
    ll ans = md((lb-la)%tb,g);
    ans = min(tola+ans,tolb)-ans;
    ans = max(ans,min(tola,tolb+md((la-lb)%ta,g)) - md((la-lb)%ta,g));
    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;
long long la,ra,ta,lb,rb,tb;
long long gcd(long long a, long long b){
	while(b != 0){
		long long r = a%b;
		a = b;
		b = r;
	}
	return a;
}
int main(){
	cin>>la>>ra>>ta>>lb>>rb>>tb;
	long long d = gcd(ta,tb);
	long long offset = floor((lb-la)/d)*d;
	long long right = min(rb,ra+offset);
	long long ans = right - lb+1ll;
	offset+=d;
	right = min(rb,ra+offset);
	ans = max(ans, right - la-offset+1ll);
	cout<<max(0ll,ans)<<endl;
}

#include<bits/stdc++.h>
#define F first
#define S second
#define ll long long
using namespace std;

const int maxn = 1e5 + 2 , inf = 2e9;
int la , ra , ta , lb , rb , tb , ans = inf , toola , toolb;

int hesab(int L1 , int R1 , int L2 , int R2){
	return max (min(R1 , R2) - max(L1 , L2) + 1 , 0);
}

int main(){
    ios::sync_with_stdio(0);
   	cin.tie(0);

	cin >> la >> ra >> ta >> lb >> rb >> tb;
	la ++;
	ra ++;
	lb++;
	rb++;

	toola = ra - la + 1;
	toolb = rb - lb + 1;

	if (ta == tb){
		cout << hesab(la , ra , lb , rb) << endl;
		return 0;
	}

	if (tb < ta){
		swap(la , lb);
		swap(ra , rb);
		swap(ta , tb);
		swap(toola , toolb);
	}

	int g = __gcd(ta , tb);
	int k = la % g;

	int j = lb / g;

	ans = min(ans , hesab(j * g + k , j * g + k + toola , lb , rb));
	ans = min(ans , hesab(j * g , j * g + toola , lb , rb));

	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma comment(linker, "/STACK:25600000000000000000000000000000000")
#define qq cout << "!!" << endl;
#define ww cout << "??" << endl;
#define rr return 0;
#define nl cout << endl;
#define pb push_back
#define mk make_pair
#define sqr(a) ((a) * (a))
#define imp(a) fixed << setprecision(a)
#define x first
#define y second
#define endl "\n"
#define lcm(a, b) (a * b / gcd(a, b))
#define int long long

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pp;
typedef pair<double, double> point;

typedef tree<
    pair<int, pp>,
    null_type,
    less<pair<int, pp> >,
    rb_tree_tag,
    tree_order_statistics_node_update>
set_t;

main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int la, ra, ta, lb, rb, tb, ans1, ans2;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int dl = abs(lb - la);
    int g = __gcd(ta, tb);
//    cout << g << endl;
    if(dl % g == 0) {
        cout << min(ra - la + 1, rb - lb + 1);
    }
    else {
        rb -= lb;
        ra -= la;
        int h = dl / g;
        int x = dl - g * h;
        ans1 = min(rb, ra - x) + 1;
//        cout << x << " " << ans1 << endl;
        x = g * (h + 1) - dl;

        ans2 = min(ra, rb - x) + 1;
//        cout << x << " " << ans2 << endl;
        cout << max(ans1, ans2);
    }
}

/*
* @Author: Nguyen Quoc Cuong
* @Mail : 97quoccuong@gmail.com
* @Created at:   2018-11-10 22:16:46
* @Last Modified time: 2018-11-10 22:40:05
*/

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) x.size()

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)

#define debug(a) cout << #a << " : " << a << endl
#define debuga(a,l,r) fore(i,l,r) cout << a[i] << ' ';cout << endl;

using namespace std;

typedef double ld;
typedef long long i64;
typedef pair<int, int> pii;
typedef pair<i64, i64> pll;
typedef vector<int> vi;
typedef vector<i64> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int,int> mii;
typedef map<i64,int> mli;

template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
template<class T> T min(T a, T b, T c) { return min(a,min(b,c)); }
template<class T> T max(T a, T b, T c) { return max(a,max(b,c)); }
void Error(string s) {cout << s << '\n';exit(0);}

void Fan_chipu();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  Fan_chipu();
}
/*====================================================== END ==============================================*/
i64 la,ra,ta,lb,rb,tb;
/*------------------------------------- */ void Fan_chipu() {
  cin >> la >> ra >> ta >> lb >> rb >> tb;
  if(ta >= ra - la + 1 or tb >= rb - lb + 1) {
    if(ta >= ra - la + 1) {
      cout << rb - lb + 1;
    } else {
      cout << ra - la + 1;
    }
    return;
  }
  if(__gcd(tb,ta) == 1) {
    cout << min(ra - la + 1,rb - lb + 1);
    return;
  }
  i64 gcd = __gcd(ta,tb);
  i64 need = lb / gcd;
  if(need * gcd + la < lb) ++need;
  i64 ans = min(rb,(need * gcd + ra)) - (need * gcd + la) + 1;
  need = la / gcd;
  if(need * gcd + lb < la) ++need;
  umax(ans,min(ra,(need * gcd + rb)) - (need * gcd + lb) + 1);
  cout << ans;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int la,ra,ta;
    int lb,rb,tb;
    scanf("%d%d%d%d%d%d",&la,&ra,&ta,&lb,&rb,&tb);
    int ans=0;
    if(ta==tb){
        if(la<=lb<=rb<=ra)
            ans=rb-lb;
        else if(lb<=la<=ra<=rb)
            ans=ra-la;
        else if(la<=lb<=ra<=rb)
            ans=ra-lb;
        else if(lb<=la<=rb<=ra)
            ans=rb-la;
        printf("%d",ans);
    }
    else{
        double x=(rb-ra)/(ta-tb);
        int t1=0,l1=0,r1=0;
        int t2=0,l2=0,r2=0;
        if(abs(ta)<abs(tb))
            t1=ta,l1=la,r1=ra,t2=tb,l2=lb,r2=rb;
        else
            t1=tb,l1=lb,r1=rb,t2=ta,l2=la,r2=ra;
        int ans1=0;
        int ans2=0;
        if(x<=0){
             ans2=x/1;
             ans1=ans2-1;
        }
        else{
             ans1=x/1;
             ans2=ans1+1;
        }
        double tmp1=t2*ans1+r2;
        double tmp11=t1*ans1+l1;
        int daan1=abs(floor(tmp1)-floor(tmp11));
        double tmp2=t1*ans2+r1;
        double tmp22=t2*ans2+l2;
        int daan2=abs(floor(tmp2)-floor(tmp22));
        int daan=max(daan1,daan2);
        //printf("%f %f %d %f %f %d ",tmp1,tmp11,daan1,tmp2,tmp22,daan2);
        printf("%d",daan);
    }
    return 0;
}

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
const int base=1e9+7;
typedef long long ll;
ll di(ll la,ll ra,ll lb,ll rb){
   if(la>rb||lb>ra)return 0;
   return min(ra,rb)-max(la,lb)+1;
}
int main() {
   ll la,lb,ra,rb,ta,tb;
   cin>>la>>ra>>ta>>lb>>rb>>tb;
   ll t=__gcd(ta,tb);
   if(abs(la-lb)%t==0){
   	cout<<min(ra-la,rb-lb)+1;
   }
   else {
   	ll k=abs(la-lb)%t;
   	ll ans1=di(k,k+ra-la,0,rb-lb),ans2=di(0,ra-la,k,k+rb-lb);
   	k=t-k;
   	ans1=max(ans1,di(k,k+ra-la,0,rb-lb)),ans2=max(ans2,di(0,ra-la,k,k+rb-lb));
   	cout<<max(ans1,ans2);
   }
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll la, ra, Ta;
  ll lb, rb, Tb;
  cin >> la >> ra >> Ta >> lb >> rb >> Tb;
  ll gcd = __gcd(Ta, Tb);
  if (gcd == 1) {
    cout << min(ra - la, rb - lb) + 1 << endl;
    exit(0);
  }
  ll best = 0;
  auto update = [&] (ll left_a, ll right_a, ll kf) -> ll {
    ll left_b = lb + kf * Tb;
    ll right_b = rb + kf * Tb;
    return min(right_b, right_a) - max(left_a, left_b) + 1;
  };
  auto check = [&] (ll kfa) -> ll {
    ll lax = la + kfa * Ta;
    ll rax = ra + kfa * Ta;
    ll l = 0, r = 1e10 + 1;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (lb + mid * Tb > rax) {
        r = mid;
      }
      else {
        l = mid;
      }
    }
    return update(lax, rax, r - 1);
  };
  if ((Ta + la - ra + 1) % (Tb + lb - rb + 1) == 0 || (Tb + lb - rb + 1) % (Ta + la - ra + 1) == 0) {
    cout << max(0ll, check(1));
  }
  else {
    cout << min(ra - la + 1, rb - lb + 1);
  }
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma comment(linker, "/STACK:25600000000000000000000000000000000")
#define qq cout << "!!" << endl;
#define ww cout << "??" << endl;
#define rr return 0;
#define nl cout << endl;
#define pb push_back
#define mk make_pair
#define sqr(a) ((a) * (a))
#define imp(a) fixed << setprecision(a)
#define x first
#define y second
#define endl "\n"
#define lcm(a, b) (a * b / gcd(a, b))
#define int long long

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pp;
typedef pair<double, double> point;

typedef tree<
    pair<int, pp>,
    null_type,
    less<pair<int, pp> >,
    rb_tree_tag,
    tree_order_statistics_node_update>
set_t;

main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int la, ra, ta, lb, rb, tb, ans1, ans2;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int dl = abs(lb - la);
    int g = __gcd(ta, tb);
//    cout << g << endl;
    if(dl % g == 0) {
        cout << min(ra - la + 1, rb - lb + 1);
    }
    else {
        rb -= lb;
        ra -= la;
        int h = dl / g;
        int x = dl - g * h;
        ans1 = min(rb, ra - x) + 1;
//        cout << x << " " << ans1 << endl;
        x = g * (h + 1) - dl;

        ans2 = min(ra, rb - x) + 1;
//        cout << x << " " << ans2 << endl;
        cout << max(ans1, max(ans2, 0ll));
    }
}

#include<stdio.h>
#include<set>
using namespace std;
set<long long> s;
    long long la, ra, ta, lb, rb, tb, za, zb, fa, fb, ans = 0, x, ha, hb, cnt, lx;
long long min(long long x, long long y)
{
    if (x > y) return y;
    return x;
}

long long gcd(long long x, long long y)
{
    if (y == 0) return x;
    return gcd(y, x % y);
}

long long jsq(long long x, long long y, long long z)
{
    if (z > 0)
        {
            if (za - z < 0) return 0; else
            return min(zb, za - z);}
    else
        {   if (zb + z < 0) return 0; else
            return min(za, zb + z);}
}
void swap(long long &x, long long &y)
{
    long long t = x;
    x = y;
    y = t;
}
int main()
{

    int i, j, k;
    scanf("%I64d%I64d%I64d", &la, &ra, &ta);
    scanf("%I64d%I64d%I64d", &lb, &rb, &tb);
    za = ra - la + 1;
    fa = la + ta - ra - 1;
    zb = rb - lb + 1;
    fb = lb + tb - rb - 1;
    ha = za + fa;
    hb = zb + fb;

    lx = gcd(ta, tb);
    if (la < lb) swap(la, lb), swap(ha, hb), swap(za, zb);
    lb += (la - lb) / hb * hb;
    while (lb <= la)
    {
        if (jsq(za, zb, lb - la) > ans) ans = jsq(za, zb, lb - la);
        lb += hb;
    }
    if (jsq(za, zb, lb - la) > ans) ans = jsq(za, zb, lb - la);

    printf("%I64d", ans);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll la, ra, Ta;
  ll lb, rb, Tb;
  cin >> la >> ra >> Ta >> lb >> rb >> Tb;
  ll best = 0;
  auto update = [&] (ll left_a, ll right_a, ll kf) -> ll {
    ll left_b = lb + kf * Tb;
    ll right_b = rb + kf * Tb;
    return min(right_b, right_a) - max(left_a, left_b) + 1;
  };
  for (int i = 0; i < 1e5; ++i) {
    ll lax = la + i * Ta;
    ll rax = ra + i * Ta;
    ll l = 0, r = 1e5 + 1;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (lb + mid * Tb >= rax) {
        r = mid;
      }
      else {
        l = mid;
      }
    }
    best = max(best, update(lax, rax, r));
    best = max(best, update(lax, rax, r - 1));

  }

  cout << best << endl;

  return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

typedef long long int ll;
typedef long double lf;

#define pll pair<ll,ll>
#define vll vector<ll>
#define vpl vector<pair<ll,ll>>
#define ppll pair<ll,pair<ll,ll>>
#define pb  push_back

#define ordered_set tree< ll , null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update >

using namespace __gnu_pbds;
using namespace std;

ll mod = 1e9+7;

ll power(ll x, ll y)
{
    ll res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res;
}

int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	ll la,ra,ta;
	ll lb,rb,tb;

	ll ans = 0 , k;

	cin>>la>>ra>>ta;
	cin>>lb>>rb>>tb;

	int cnt = 10000000;

	if(lb < la){
	        swap(la,lb);
    	    swap(ra,rb);
    	    swap(ta,tb);
	}

	k = 0;

	if(lb >= ra){
	    k = ceil((lf)(lb-ra)/(lf)ta);
	}

	ra += k*ta; la += k*ta;

	int flag = 0;

	while(cnt--){

	    //cout<<la<<" "<<ra<<" "<<lb<<" "<<rb<<endl;
	    ll lower_val = max(la , lb);
	    ll upper_val = min(ra , rb );

	   if(upper_val>=lower_val){
	       ans = max( ans , abs(upper_val - lower_val )+1 );
	   }

	   ra += ta; la += ta;

	   if(lb < la){
	        swap(la,lb);
    	    swap(ra,rb);
    	    swap(ta,tb);
	    }

    }

	cout<<ans;

}

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll check(ll abegin,ll aend,ll bbegin,ll bend){
  if(abegin<=bbegin){
    if(aend<=bend&&aend>=bbegin){
      return aend-bbegin+1;
    }
    else if(aend>=bend){
      return bend-bbegin+1;
    }
  }
  else{
    if(bend<=aend&&bend>=abegin){
      return bend-abegin+1;
    }
    else if(bend>=aend){
      return aend-abegin+1;
    }
  }
  return 0;
}
int main()
{
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    // ll t;
    // cin>>t;
    // while(t--)
    {
        ll i,j,k,n,m,ans=0,cnt=0,sum=0;
        ll la,ra,ta,lb,rb,tb;
        cin>>la>>ra>>ta>>lb>>rb>>tb;
        ll a,b;
        if(ra+1>=la+ta){
          a=1e9-la;
        }
        else{
          a=ra-la;
        }
        if(rb+1>=lb+tb){
          b=1e9-lb;
        }
        else{
          b=rb-lb;
        }
        // cout<<la<<" "<<la+a<<'\n';
        // cout<<lb<<" "<<lb+b<<'\n';
        if(la<=lb){
          i=lb-la;
          i/=ta;
          la+=i*ta;
          for(i=0;i<20;i++){
            ans=max(ans,check(la+i*ta,la+a+i*ta,lb,lb+b));
            // cout<<ans<<'\n';
          }
          // ans=max(ans,check(la,la+a,lb,lb+b));
        }
        else{
          i=la-lb;
          i/=tb;
          lb+=i*tb;
          for(i=0;i<20;i++){
            ans=max(ans,check(lb+i*tb,lb+b+i*tb,la,la+a));
          }
        }
        cout<<ans;
    }
}

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

ll prueba(ll la,ll ra,ll ta, ll lb, ll rb, ll tb){
	//Ponemos B en cero
	ll res = 0;
	ll l = lb;
	ll r = rb;

	ll k = (l-la)/ta;
	k = max(0ll,k);
	forn(i,k,1000000){
		ll l2 = la+ll(i)*ta;
		ll r2 = ra+ll(i)*ta;
		ll days = min(r,r2)-max(l,l2)+1;
		res = max(res,days);
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll la,ra,ta,lb,rb,tb;
	cin >> la >> ra >> ta >> lb >> rb >> tb;
	ll res = max(prueba(la,ra,ta,lb,rb,tb),prueba(lb,rb,tb,la,ra,ta));
	cout << res << '\n';
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/

//
//  Created by Ruslan Jankurazov.
//  Copyright © 2018 Ruslan Jankurazov. All rights reserved.
//

#pragma GCC optimize("Ofast")

#include "bits/stdc++.h"

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define uint unsigned int
#define pii pair <int, int>
#define pll pair <ll, ll>
#define sqr(x) (x)*(x)

using namespace std;

ll bp(ll a, ll b, ll mod) {
    if (!b)
        return 1;
    if (b&1)
        return a * bp(a, b - 1, mod) % mod;
    ll x = bp(a, b/2, mod);
    return x*x % mod;
}

ll phi (ll n) {
    ll result = n;
    for (ll i=2; i*i<=n; ++i)
        if (n % i == 0) {
            while (n % i == 0)
                n /= i;
            result -= result / i;
        }
    if (n > 1)
        result -= result / n;
    return result;
}

int main() {
#define FILE_INPUT 0
#define FILE ""
#ifndef DEBUG
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#if FILE_INPUT == 1
#warning "FREOPEN is on"
    freopen(FILE".in", "r", stdin);
    freopen(FILE".out", "w", stdout);
#endif
#endif
    ll l, r, t;
    ll a, b, c;
    cin >> l >> r >> t;
    cin >> a >> b >> c;
    ll mx = 0;
    ll pre = (r - b)%c;
    ll k = (c - pre + c)%c * bp(t, phi(c) - 1, c)%c;
    //cout << k << '\n';
    ll d = (r + k*t - b)/c;
    mx = max(mx, (b + d*c) - max(l + k*t, a + d*c));
    cout << max(mx, (b + d*c) - max(l + k*t, a + d*c));;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t1,m,y,z,a,b,t2,c,d;
    cin>>a>>b>>t1;
    cin>>c>>d>>t2;
    if(c<a){
        swap(a,c);
        swap(b,d);
        swap(t1,t2);
    }
    b-=a;
    c-=a;
    d-=a;
    a-=a;
    x = 0;
    x = max(x, max(0ll, min(b,d)- c + 1));
    debug(x);
    m = gcd(t1,t2);
    m--;
    d-=(c-m);
    c = m;
    x = max(x, max(0ll, min(b,d)- c + 1));
    cout<<x;

    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a1,b1,t1,a2,b2,t2;
    cin>>a1>>b1>>t1>>a2>>b2>>t2;
    long long skok = __gcd(t1,t2);
    long long min1;
    long long min2;
    long long d1,d2,d;
    if(a2>a1)
    {
        d1=(a2-a1)%skok;
        d2=abs(d1-skok);
    }
    else
    {
        d2=(a1-a2)%skok;
        d1=abs(d1-skok);
    }
    //cout<<d1<<" "<<d2<<endl;
        min1 = max(0LL,min(b1-a1+1-d1,b2-a2+1));

        min2 = max(0LL,min(b2-a2+1-d2,b1-a1+1));
    cout<<max(min1,min2);
}

#include <iostream>

using namespace std;

template <typename T>
T gcd(T a, T b) {
    if (a > b) return gcd(b, a);
    return a == 0 ? b : gcd(b % a, a);
}

int main() {
    int l[2], r[2], t[2];
    for (int i = 0; i < 2; ++i) {
        cin >> l[i] >> r[i] >> t[i];
        ++r[i];
    }

    int s = gcd(t[0], t[1]);

    if (l[1] < l[0]) {
        swap(l[0], l[1]);
        swap(r[0], r[1]);
    }

    for (int i = 1; i >= 0; --i) {
        r[i] -= l[0];
        l[i] -= l[0];
    }

    int sl = l[1] - l[1] % s;
    l[1] -= sl, r[1] -= sl;

    int ans = min(r[0], r[1]) - l[1];
    l[1] -= s, r[1] -= s;
    ans = max(ans, min(r[0], r[1]) - l[0]);

    cout << ans << endl;
    return 0;
}

//Author: Artem Romanov
#include <bits/stdc++.h>
//#define TASK "file"

#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef long double dbl;

namespace task {
int64_t la, ra, ta;
int64_t lb, rb, tb;
int64_t lc, rc, tc;
int64_t dl, dr, gg;
int64_t aa, bb, cc;

int main() {
  cin >> la >> ra >> ta;
  cin >> lb >> rb >> tb;
  ++ra; ++rb;
  if (ra == ta) {
    return cout << rb - lb << '\n', 0;
  } else if (rb == tb) {
    return cout << ra - la << '\n', 0;
  }
  gg = __gcd(ta, tb);
  if (gg == 1) {
    return cout << min(ra - la, rb - lb) << '\n', 0;
  }
  dr = (gg + (lb - la) % gg) % gg;
  dl = dr - gg;
  lc = la + dr;
  rc = rb - lb + lc;
  tc = tb;
  aa = max(int64_t(0), min(rc, ra) - max(lc, la));
  lc += tc;
  rc += tc;
  aa = max(aa, min(rc, ra) - max(lc, la));
  lc = la + dl;
  rc = rb - lb + lc;
  tc = tb;
  bb = max(int64_t(0), min(rc, ra) - max(lc, la));
  lc -= tc;
  rc -= tc;
  bb = max(bb, min(rc, ra) - max(lc, la));
  cc = max(aa, bb);
  cout << cc << '\n';
  return 0;
}
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(11);
  cout.setf(ios::fixed);
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#elif defined(TASK)
  freopen(TASK".in", "r", stdin);
  freopen(TASK".out", "w", stdout);
#endif
  return task::main();
}

// Author - Ranadev Varma
// FML
// codechef Handle   - ranadev
// codeforces Handle - Ranadev_CaptainR

#pragma GCC optimize("O3")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> lol_set;

// *find_by_order(k) gives kth largest element
// order_of_key(k) gives number of elements strictly smaller than our element

#define PB push_back
#define MP make_pair
#define LL long long int
#define FOR(i,a,b) for(long long int i = a ; i < b ; i++)
#define RFOR(i,a,b) for(long long int i = a ; i >= b ; i--)
#define FI first
#define SE second

const LL MOD = 1000000007;
const long double PI = 3.14159265359;
const LL INF = 1e18;

LL gcd(LL a,LL b)
{
	if(a==0)
	{
		return b;
	}
	return gcd(b%a,a);
}

LL exp(LL b,LL p)
{
	if(p==0)
	{
		return 1;
	}
	else if(p%2==0)
	{
		return exp(b*b,p/2);
	}
	else
	{
		return b*exp(b*b,(p-1)/2);
	}
}

LL mexp(LL b,LL p)
{
	if(p==0)
	{
		return 1;
	}
	else if(p%2==0)
	{
		return exp(b*b,p/2)%MOD;
	}
	else
	{
		return (b*exp(b*b,(p-1)/2))%MOD;
	}
}

LL minv(LL a)
{
	return mexp(a,MOD-2);
}

int isprime(LL n)
{
	for(LL i = 2 ; i*i<=n ; i++)
	{
		if(n%i==0)
		{
			return 0;
		}
	}
	return 1;
}

std::set<int>::iterator it;

int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	cout.tie(0);

  	srand(time(NULL));

  	LL la,ra,ta,lb,rb,tb;
  	cin >> la >> ra >> ta >> lb >> rb >> tb;

  	LL ans = 0;

	if(lb>=la)
	{

		ans = max((LL)0,min(rb,ra)-max(la,lb)+1);

		LL diff = lb-la;
		LL mul = (diff+ta-1)/ta;

		if(mul>=1)
		{
			mul--;
		}

		la += (mul)*ta;
		ra += (mul)*ta;

		ans = max(ans,min(rb,ra)-max(la,lb)+1);

		LL tla = la,tlb = lb,tra = ra,trb = rb,cnt=0;

		tla += ta;
		tra += ta;

		ans = max(ans,min(trb,tra)-max(tla,tlb)+1);

		while(1)
		{

			mul = abs(rand()%100000000);

			tlb = lb + mul*tb;
			trb = rb + mul*tb;

			diff = tlb-tla;
			mul = (diff+ta-1)/ta;

			if(mul>=1)
			{
				mul--;
			}

			tla += mul*ta;
			tra += mul*ta;

			ans = max(ans,min(trb,tra)-max(tla,tlb)+1);

			cnt++;
			if(cnt>10000000)
			{
				break;
			}
		}

		cout << ans << "\n";
	}
	else
	{

		ans = max((LL)0,min(rb,ra)-max(la,lb)+1);

		LL diff = la-lb;
		LL mul = (diff+tb-1)/tb;

		lb += (mul)*tb;
		rb += (mul)*tb;

		ans = max(ans,min(rb,ra)-max(la,lb)+1);

		LL tla = la,tlb = lb,tra = ra,trb = rb,cnt=0;

		tla += ta;
		tra += ta;

		ans = max(ans,min(trb,tra)-max(tla,tlb)+1);

		ans = max(ans,min(trb,tra)-max(tla,tlb)+1);

		while(1)
		{

			mul = abs(rand()%100000000);

			tla = la + mul*ta;
			tra = ra + mul*ta;

			diff = tla-tlb;
			mul = (diff+tb-1)/tb;

			if(mul>=1)
			{
				mul--;
			}

			tlb += mul*tb;
			trb += mul*tb;

			ans = max(ans,min(trb,tra)-max(tla,tlb)+1);

			cnt++;
			if(cnt>10000000)
			{
				break;
			}
		}

		cout << ans << "\n";
		return 0;
	}
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const long long INF = 1000000000*100000000;
int gcd(int a,int b){

    if (a<b){
        swap(a,b);
    }
    if (b==0){
        return a;
    }

    return gcd(b,a%b);

}

int main() {
    int l,r,t,l1,r1,t1;
    cin >> l >> r >> t >> l1 >> r1 >> t1;
    int a=__gcd(t,t1);
    int co=((l-l1)%a+a)%a;
    int co1=((l1-l)%a+a)%a;
    int mx=min(r1-l1-co,r-l)+1;
    int mx1=min(r-l-co1,r1-l1)+1;
    cout << max(mx,mx1);

    return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int la,ra,ta,lb,rb,tb;
    scanf("%d%d%d%d%d%d",&la,&ra,&ta,&lb,&rb,&tb);
    if(ta<tb)
    {
        swap(la,lb);
        swap(ra,rb);
        swap(ta,tb);
    }
    int p=__gcd(ta,tb);
    int x=la/tb;
    int st=lb+x*tb,en=rb+x*tb;
    int ans=0,l,r;
    while(en<=ra)
    {
        if(st>la) break;
        l=max(st,la);
        r=min(en,ra);
        ans=max(ans,r-l+1);
        st+=p;
        en+=p;
    }
    l=max(st,la);
    r=min(en,ra);
    ans=max(ans,r-l+1);
    printf("%d\n",ans);
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long la, ra, ta;
    long long lb, rb, tb;

    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;

    if(ta < tb)
    {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }

    long long k = __gcd(ta, tb);

    ra -= la;
    lb -= la;
    rb -= la;
    la = 0;

    long long pos = lb % tb;
    if(pos < 0)
        pos += tb;

    pos %= k;

    long long sol1 = min(rb - lb + 1, ra - pos + 1);
    long long sol2 = max(0LL, pos - tb + (rb - lb) - la + 1);

    long long sol = max(sol1, sol2);

    cout << sol << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=3e5+7;

struct state
{
    ll l,r,t;
}a,b;

ll overlap( ll a , ll b , ll c , ll d )
{
    return max( 0ll , min(b,d)-max(a,c)+1 );
}

int main()
{
    ll ans=0;
    cin>>a.l>>a.r>>a.t;
    cin>>b.l>>b.r>>b.t;
    ll g = __gcd(a.t,b.t);
    ll d = abs(a.l-b.l);
    d%=g;
    //cout<<overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-a.r+1 ) )<<endl;
    ans = max( overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-b.l+1 ) ) , overlap( b.l+d , b.l+d + (a.r-a.l+1) , b.l , b.r ) );
    d-=g;
    d*=-1;
    ans = max( overlap( a.l , a.r , a.l+d , a.l+d + ( b.r-b.l+1 ) ) , ans );
    ans = max( overlap( b.l+d , b.l+d + (a.r-a.l+1) , b.l , b.r ) , ans );
    cout<<ans;

    return 0;
}

// Author - Ranadev Varma
// FML
// codechef Handle   - ranadev
// codeforces Handle - Ranadev_CaptainR

#pragma GCC optimize("O3")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> lol_set;

// *find_by_order(k) gives kth largest element
// order_of_key(k) gives number of elements strictly smaller than our element

#define PB push_back
#define MP make_pair
#define LL long long int
#define FOR(i,a,b) for(long long int i = a ; i < b ; i++)
#define RFOR(i,a,b) for(long long int i = a ; i >= b ; i--)
#define FI first
#define SE second

const LL MOD = 1000000007;
const long double PI = 3.14159265359;
const LL INF = 1e18;

LL gcd(LL a,LL b)
{
	if(a==0)
	{
		return b;
	}
	return gcd(b%a,a);
}

LL exp(LL b,LL p)
{
	if(p==0)
	{
		return 1;
	}
	else if(p%2==0)
	{
		return exp(b*b,p/2);
	}
	else
	{
		return b*exp(b*b,(p-1)/2);
	}
}

LL mexp(LL b,LL p)
{
	if(p==0)
	{
		return 1;
	}
	else if(p%2==0)
	{
		return exp(b*b,p/2)%MOD;
	}
	else
	{
		return (b*exp(b*b,(p-1)/2))%MOD;
	}
}

LL minv(LL a)
{
	return mexp(a,MOD-2);
}

int isprime(LL n)
{
	for(LL i = 2 ; i*i<=n ; i++)
	{
		if(n%i==0)
		{
			return 0;
		}
	}
	return 1;
}

std::set<int>::iterator it;

int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	cout.tie(0);

  	//srand(TIME(NULL));

  	LL la,ra,ta,lb,rb,tb;
  	cin >> la >> ra >> ta >> lb >> rb >> tb;

  	if(ra==la && rb==lb)
  	{
  		cout << "1" << "\n";
  		return 0;
  	}

  	LL ans = 0;

	if(lb>=la)
	{

		ans = max((LL)0,min(rb,ra)-max(la,lb)+1);

		LL diff = lb-la;
		LL mul = (diff+ta-1)/ta;

		la += (mul-1)*ta;
		ra += (mul-1)*ta;

		LL tla = la,tlb = lb,tra = ra,trb = rb;

		FOR(i,0,10000)
		{
			tla = la + (5000-i)*ta;
			tra = ra + (5000-i)*ta;

			FOR(j,0,10000)
			{
				tlb = lb + (5000-j)*tb;
				trb = rb + (5000-j)*tb;

				ans = max(ans,min(trb,tra)-max(tla,tlb)+1);
			}
		}

		cout << ans << "\n";
	}
	else
	{

		ans = max((LL)0,min(rb,ra)-max(la,lb)+1);

		LL diff = la-lb;
		LL mul = (diff+tb-1)/tb;

		lb += (mul-1)*tb;
		rb += (mul-1)*tb;

		LL tla = la,tlb = lb,tra = ra,trb = rb;

		FOR(i,0,10000)
		{
			tla = la + (5000-i)*ta;
			tra = ra + (5000-i)*ta;
			FOR(j,0,10000)
			{
				tlb = lb + (5000-j)*tb;
				trb = rb + (5000-j)*tb;

				ans = max(ans,min(trb,tra)-max(tla,tlb)+1);
			}
		}

		cout << ans << "\n";
		return 0;
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define sc(x) scanf("%d", &x)
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ALL(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())
#define sqr(x) ((x)*(x))
#define ABS(x) ((x)>=0?(x):(-(x)))
#define fastio ios::sync_with_stdio(0),cin.tie(0)
template<class T>T gcd(T a, T b){return b?gcd(b, a%b):a;}

int main()
{
    ll la, ra, ta;
    ll lb, rb, tb;
    scanf("%lld%lld%lld%lld%lld%lld", &la, &ra, &ta, &lb, &rb, &tb);
    if(ta > tb)swap(la, lb), swap(ra, rb), swap(ta, tb);
    ll g = gcd(ta, tb);
    ll ans = 0;
    ll t1, t2, k;
    k = max(0LL, (lb-la+g-1)/g);
    t1 = la+ta*k, t2 = ra+ta*k;
    ans = max(ans, min(rb, t2)-max(lb, t1)+1);
    k = max(0LL, (rb-ra)/g);
    t1 = la+ta*k, t2 = ra+ta*k;
    ans = max(ans, min(rb, t2)-max(lb, t1)+1);
    printf("%lld\n", ans);
    return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
#define repVector(v)  for( auto it = v.begin(); it != v.last(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define OUTFILE freopen("output.out","w",stdout)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll gcd(ll n1, ll n2) {
	if (n2 != 0) return gcd(n2, n1 % n2);
	else return n1;
}

ll join(ll l1, ll r1, ll l2, ll r2) {
	ll l = max(l1, l2);
	ll r = min(r1, r2);
	return r - l + 1;
}

int main() {
	ll la, ra, ta;
	cin >> la >> ra >> ta;

	ll lb, rb, tb;
	cin >> lb >> rb >> tb;

	if(lb < la) {
		swap(la, lb);
		swap(ra, rb);
		swap(ta, tb);
	}

	ll rangea = ra - la;
	ll rangeb = rb - lb;

	ll ans = 0;

	ll g = gcd(ta, tb);
	debug(g);
	ll delta = 100000;
	ll k = (lb - la) / g;

	debug(k);

	FOR(i, k - delta, k + delta) {
		ll l = (lb - la) + i * g;
		ll r = l + rangeb;
		ans = max(ans, join(0, rangea, l, r));
	}

	cout << ans;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <vector>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int maxn = 10000000 + 5;

int gcd(int a, int b){return b ? gcd(b, a % b) : a;}

int la, ra, ta, lb, rb, tb;

int cal(int la, int ra, int lb, int rb){
    int mn = max(la, lb), mx = min(ra, rb);
    return max(0, mx - mn + 1);
}

int main(){
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    // if (ta - 1 == ra) return printf("%d\n", rb - lb + 1), 0;
    // if (tb - 1 == rb) return printf("%d\n", ra - la + 1), 0;

    if (rb - lb > ra - la) swap(la, lb), swap(ra, rb), swap(ta, tb);

    int a = la, b = ra; vector<PII> v;
    if (la < lb){
        int d = (lb - la) / ta;
        a += d * ta; b += d * ta;
    }
    for (int i = 0; i < maxn; i++){
        if (b >= (int)1e9) break;
        v.emplace_back(a, b);
        a += ta; b += ta;
    }
    a = lb, b = rb;
    int ans = 0;
    for (int i = 0; i < maxn; i++){
        if (b >= (int)1e9) break;
        PII x = make_pair(a, b);
        int r = lower_bound(v.begin(), v.end(), x) - v.begin();
        // cout << v[r].first << ' ' << v[r].second << endl;
        if (r < v.size()){
            ans = max(ans, cal(a, b, v[r].first, v[r].second));
        }
        if (r + 1 < v.size()){
            ans = max(ans, cal(a, b, v[r + 1].first, v[r + 1].second));
        }
        if (r > 0) {
            ans = max(ans, cal(a, b, v[r - 1].first, v[r - 1].second));
        }
        a += tb; b += tb;
    }
    cout << ans;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(args...)                             \
    do                                           \
    {                                            \
        cout << "\033[32;1m" << #args << " -> "; \
        err(args);                               \
    } while (0)
#else
#define dbg(...)
#endif
void err()
{
    cout << "\033[39;0m" << endl;
}
template <template <typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args)
{
    for (auto x : a) cout << x << ' ';
    err(args...);
}
template <typename T, typename... Args>
void err(T a, Args... args)
{
    cout << a << ' ';
    err(args...);
}
/****************************************************************************************************/

typedef long long ll;

int main()
{
    ll la, ra, ta;
    ll lb, rb, tb;
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;
    vector<pair<ll, int>> vec;
    for (int i = -la / ta; i < -la / ta + 100000; i++)
        vec.emplace_back(la + ta * i, 1), vec.emplace_back(ra + ta * i + 1, -1);
    for (int i = -lb / tb; i < -lb / tb + 100000; i++)
        vec.emplace_back(lb + tb * i, 1), vec.emplace_back(rb + tb * i + 1, -1);
    sort(vec.begin(), vec.end());
    ll cnt = 0, pc = 0, pre = 0;
    ll ans = 0;
    for (auto& t : vec)
    {
        pc = cnt;
        cnt += t.second;
        if (cnt == 1 && pc == 2) ans = max(ans, t.first - pre);
        pre = t.first;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair

ll Solve(ll la, ll ra, ll ta, ll lb, ll rb, ll tb){
  ll g = __gcd(ta, tb);
  ll k = max(0ll, (la - lb + g - 1) / g);
  ll x = lb - la + k * g;

  if(x > ra - la){
    return 0ll;
  }

  k = (ra - la - x) / tb;
  x += k * tb;

  return max(0ll, k - 1) * (rb - lb + 1) + (min(ra - la, x + rb - lb) - x + 1);
}

int main(){
  ll la, ra, ta, lb, rb, tb;

  cin >> la >> ra >> ta >> lb >> rb >> tb;

  cout << max(Solve(la, ra, ta, lb, rb, tb), Solve(lb, rb, tb, la, ra, ta)) << '\n';

  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <memory.h>
#include <map>
#include <string>
#include <ctime>
#include <cstring>
#include <cmath>
int n, m;
using namespace std;
const long long maxn = 2e5 + 5;
const long long mod = 1000000007;
static long long obrf[maxn], f[maxn];
static int parent[maxn], siz[maxn], Rank[maxn];
static long long aa[maxn];
static long long kek[maxn];
long long pew(long long a, long long step)
{
	if (step == 0)
	{
		return 1;
	}
	long long b = pew((a*a) % mod, step / 2);
	if (step % 2 == 0)
	{
		return b;
	}
	else
	{
		return (b*a) % mod;
	}
}

void precal(int cnt)
{
	long long tmp = 1;
	f[0] = 1;
	obrf[0] = 1;
	for (int i = 1; i <= cnt; i++)
	{
		tmp *= i;
		tmp %= mod;
		f[i] = tmp;
		obrf[i] = pew(tmp, mod - 2);
	}
}

void make_set(int v) {
	parent[v] = v;
	Rank[v] = 0;
	siz[v] = 1;
}

int find_set(int v) {
	if (v == parent[v])
		return v;
	return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
	a = find_set(a);
	b = find_set(b);
	if (a != b) {
		if (Rank[a] < Rank[b])
			swap(a, b);
		parent[b] = a;
		siz[a] += siz[b];
		if (Rank[a] == Rank[b])
			++Rank[a];
	}
}
long long gcd(long long ll, long long rr)
{
	if (ll < rr)
		return gcd(rr, ll);
	if (rr == 0)
		return ll;
	return gcd(ll%rr, rr);
}

/*void QSort(int ll, int rr)
{
int ee = ll;
int ff = rr;

long long q = rand();
q = c[ll+q%(rr-ll+1)];
while (ee <= ff)
{
while (c[ee] < q) ee++;
while (c[ff] > q) ff--;
if (ee <= ff)
{
swap(c[ee], c[ff]);
swap(s[ee], s[ff]);
ee++;
ff--;
}
}
if (ff > ll) QSort(ll, ff);
if (ee < rr) QSort(ee, rr);
}*/

int main()
{
	ios_base::sync_with_stdio(false);
//	freopen("a1.in", "r", stdin);
//	freopen("a1.out", "w", stdout);
	long long la, ra, ta;
	long long lb, rb, tb;
	long long t, ans;
	cin >> la >> ra >> ta >> lb >> rb >> tb;
	if (ra - la > rb - lb)
	{
		swap(ra, rb);
		swap(la, lb);
		swap(ta, tb);
	}
	t = gcd(ta, tb);

	if (la<lb)
	{
		ra += ((lb - la) / t)*t;
		la += ((lb - la) / t)*t;
		ans = min(rb, ra) - max(lb, la) + 1;
		ra += t;
		la += t;
		if (ans < min(rb, ra) - max(lb, la) + 1)
		{
			ans = min(rb, ra) - max(lb, la) + 1;
		}
	}
	else
	{
		rb += ((la - lb) / t)*t;
		lb += ((la - lb) / t)*t;
		ans = min(rb, ra) - max(lb, la) + 1;
		if (lb < la)
		{
			lb += t;
			rb += t;
		}
		if (ans < min(rb, ra) - max(lb, la) + 1)
		{
			ans = min(rb, ra) - max(lb, la) + 1;
		}
	}
	cout << ans;
}

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include "ext/pb_ds/tree_policy.hpp"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/rope"

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define f first
#define s second

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
ll la, ra, ta, lb, rb, tb;
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    if(la > lb) swap(la, lb), swap(ta, tb), swap(ra, rb);
    ll g = __gcd(ta, tb);
    la -= (la / g) * g; ra -= (la / g) * g;
    lb -= (lb / g) * g; rb -= (lb / g) * g;
    cout << max(0LL, max(min(ra, rb) - max(la, lb) + 1, min(ra + g, rb) - max(la + g, lb) + 1));
    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(long long i=a;i<=n;++i)
#define dbg1(a,b,c) cout<<"\n->"<<a<<" "<<b<<" "<<c<<"\n";
#define dbg2(a,b,c) cout<<"\n--"<<a<<" "<<b<<" "<<c<<"\n";
using namespace std;

typedef long long ll;
typedef long double ld;

ll mod=(ll)1e9+7;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

    ll la,ra,ta,lb,rb,tb;
    cin>>la>>ra>>ta>>lb>>rb>>tb;
    ll g=__gcd(ta,tb);
    ll xx=(lb-la);
    if(xx%g==0){
        cout<<1+min(ra-la,rb-lb);
        return 0;
    }
    ll m=xx%g;
    ll two=ra-la-m+1;

    m=g-m;
    ll one=rb-lb-m+1;

    cout<<max(one,two);
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <fstream>
#include <iomanip>

#define ll long long
#define ld long double

using namespace std;
vector<vector<int>> g;
vector<bool> used;

ll nod(ll a, ll b) {
    if (b == 0)
        return a;
    return nod(b, a % b);
}

int main() {
    //ifstream cin("labirint.in");
    //ofstream cout("labirint.out");
    ll la, ra, ta, lb, rb, tb;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    ll t = nod(ta, tb);
    ll s;
    if (la > lb) {
        s = lb;
        lb = la;
        la = s;

        s = rb;
        rb = ra;
        ra = s;
    }
    ll k = (lb - la) / t;
    ll ans1 = min(k * t + ra, lb + rb) - lb + 1;
    k++;
    ll ans2 = min(k * t + ra, rb) - (k * t + la) + 1;
    cout << max(ans1, ans2);
}

#include <bits/stdc++.h>
#define int long long
using namespace std;

long a, b, ta;
long c, d, tb;

signed main() {
    //freopen("input.txt","r",stdin);

    cin >> a >> b >> ta ;
    cin >> c >> d >> tb;
	long g = __gcd(ta, tb);

	long ans = 0;

	if (a > c) {
		swap(a, c);
		swap(b, d);
		swap(ta, tb);
	}

	long bst = (c - a) / g;
	//cout << c-a << endl;
	//cout << g << endl;
	a += bst * g; b += bst * g;
    cout <<  bst << endl;
	for (int r = 0; r < min(a,b)/g; r++) {
		ans = max(ans, min(d, b) - max(a, c) + 1);
		a += g; b += g;
	}

	cout << ans << endl;

}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
  ll la, ra, ta, lb, rb, tb;
  cin >> la >> ra >> ta >> lb >> rb >> tb;
  ll g = gcd(ta, tb);
  if (la > lb) {
    swap(la, lb);
    swap(ra, rb);
    swap(ta, tb);
  }
  ll diff = ((lb - la) / g) * g;
  ll sol = min(ra + diff, rb) - lb + 1;
  sol = max(sol, min(ra + diff + g, rb) - (la + g) + 1);
  sol = max(sol, (ll)0);
  cout << sol << endl;
  return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn=1e5+100;
const int mod=1e9+7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll la, ra, ta, lb, rb, tb;
int A[maxn], B[maxn];
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin>>la>>ra>>ta;
    cin>>lb>>rb>>tb;
    ll g=__gcd(ta, tb);
    if(g == 1)
    {
        cout<<min(ra-la, rb-lb)+1<<"\n";
        exit(0);
    }
    else
    {
        ll ans=0;
        if(tb/g < ta/g)
        {
            for(int i=0; i<tb/g; i++)
            {
                if(clock() >= 950)break;
                ll l=la+i*ta, r=ra+i*ta;
                ll x=(r-rb)/tb;
                ll L=lb+x*tb, R=rb+x*tb;
                ans=max(ans, min(R, r)-max(L, l)+1);
                L+=tb, R+=tb;
                ans=max(ans, min(R, r)-max(L, l)+1);
            }
        }
        else
        {
            swap(la, lb);
            swap(ra, rb);
            swap(ta, tb);
            for(int i=0; i<tb/g; i++)
            {
                if(clock() >= 950)break;
                ll l=la+i*ta, r=ra+i*ta;
                ll x=(r-rb)/tb;
                ll L=lb+x*tb, R=rb+x*tb;
                ans=max(ans, min(R, r)-max(L, l)+1);
                L+=tb, R+=tb;
                ans=max(ans, min(R, r)-max(L, l)+1);
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll la, ra, Ta;
  ll lb, rb, Tb;
  cin >> la >> ra >> Ta >> lb >> rb >> Tb;
  ll best = 0;
  auto update = [&] (ll left_a, ll right_a, ll kf) -> ll {
    ll left_b = lb + kf * Tb;
    ll right_b = rb + kf * Tb;
    return min(right_b, right_a) - max(left_a, left_b) + 1;
  };
  auto check = [&] (ll kfa) -> ll {
    ll lax = la + kfa * Ta;
    ll rax = ra + kfa * Ta;
    ll l = 0, r = 1e10 + 1;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (lb + mid * Tb > rax) {
        r = mid;
      }
      else {
        l = mid;
      }
    }
    return update(lax, rax, r - 1);
  };
  ll l = 0, r = 1e9;

  while (r - l > 5) {
    ll lm = l + (r - l) / 3;
    ll rm = r - (r - l) / 3;
    if (check(lm) > check(rm)) {
      r = rm;
    }
    else {
      l = lm;
    }
  }
  for (int i = l; i <= r; ++i) {
    best = max(best, check(i));
  }
  cout << best << endl;

  return 0;
}

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;

/************************************/

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<double> VD;
typedef vector<VB> VVB;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef vector<string> VS;

template <typename T>
using us = unordered_set<T>;

template <typename K, typename V>
using um = unordered_map<K, V>;

/************************************/

const int MOD = 1e9 + 7;
const double EPS = 1e-6;

/************************************/

int gcd(int x, int y) {
    return x ? gcd(y % x, x) : y;
}

int lcm(int x, int y) {
    return x * y / gcd(x, y);
}

int powerMOD(int x, int y) {
    if (!y) return 1;
    LL p = powerMOD(x, y / 2) % MOD;
    p = (p * p) % MOD;
    return y % 2 ? (x * p) % MOD : p;
}

int modInverse(int x) {
    return powerMOD(x, MOD - 2);
}

int binCoef(int n, int k) {
    LL ans = n - k + 1;
    for (int i = 2; i <= k; i++) {
        ans = ans * (n - k + i) % MOD;
        ans = ans * modInverse(i) % MOD;
    }
    return ans;
}

bool isPrime(LL x) {
    if (x % 2 == 0) return x == 2;
    for (LL i = 3; i <= sqrt(x); i += 2) {
        if (x % i == 0) return false;
    }
    return true;
}

/************************************/

template <typename F, typename S>
string to_string(pair<F, S> p) {
    return to_string(p.first) + ',' + to_string(p.second);
}

template <typename T>
string to_string(vector<T>& v) {
    ostringstream oss;
    auto out = ostream_iterator<T>(oss, ", ");
    if (!v.empty()) {
        copy(v.begin(), v.end() - 1, out);
        oss << v.back();
    }
    return oss.str();
}

/************************************/

template <typename F, typename S>
ostream& operator<<(ostream& os, const pair<F, S>& p) {
    return os << '(' << p.first << ", " << p.second << ')';
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '[';
    typename vector<T>::const_iterator it;
    for (it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) os << ", ";
        os << *it;
    }
    return os << ']';
}

template <typename T>
ostream& operator<<(ostream& os, const us<T>& v) {
    os << '{';
    typename us<T>::const_iterator it;
    for (it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) os << ", ";
        os << *it;
    }
    return os << '}';
}

template <typename K, typename V>
ostream& operator<<(ostream& os, const um<K, V>& m) {
    os << '{';
    typename um<K, V>::const_iterator it;
    for (it = m.begin(); it != m.end(); it++) {
        if (it != m.begin()) os << ", ";
        os << it->first << "-->" << it->second;
    }
    return os << '}';
}

#define deb(x) cerr << #x << " = " << x << endl;

/************************************/

int main() {
    ios::sync_with_stdio(false);
    int la, ra, ta, lb, rb, tb, ans = 0;
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    int g = gcd(ta, tb);
    int diff = (lb - la - 1) / g * g;
    la += diff;
    ra += diff;
    ans = max(ans, min(ra, rb) - max(la, lb) + 1);
    diff = ((lb - la - 1) / g + 1) * g;
    la += diff;
    ra += diff;
    ans = max(ans, min(ra, rb) - max(la, lb) + 1);
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

long long res (long long al, long long ar, long long bl, long long br) {
    return (min(ar, br) - max(al, bl) + 1);
}

int main() {
    long long al, ar, ta; cin >> al >> ar >> ta;
    long long bl, br, tb; cin >> bl >> br >> tb;

    if (al > ar) {
        swap(al, bl);
        swap(br, ar);
        swap(ta, tb);
    }

    long long g = __gcd(ta, tb);

    long long ga = al - (al % g);
    al -= ga;
    ar -= ga;

    long long gb = bl - (bl % g);
    bl -= gb;
    br -= gb;

    long long result = res(al, ar, bl, br);

    result = max(result, res(al + g, ar + g, bl, br));

    result = max(result, res(al - g, ar - g, bl, br));

    result = max(result, res(al + g + g, ar + g + g, bl, br));

    result = max(result, res(al - g - g, ar - g - g, bl, br));

    ga = ar - (ar % g);
    ar -= ga;
    al -= ga;

    gb = br - (br % g);
    br -= gb;
    bl -= gb;

    result = max(result, res(al, ar, bl, br));

    result = max(result, res(al + g, ar + g, bl, br));

    result = max(result, res(al - g, ar - g, bl, br));

result = max(result, res(al + g + g, ar + g + g, bl, br));

    result = max(result, res(al - g - g, ar - g - g, bl, br));

    cout << result << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int ta, la, ra, tb, lb, rb, g;

int gcd(int x, int y) {
	return (!y) ? x : gcd(y, x % y);
}

int get(int a, int b, int c, int d) {
	a = max(a, c);
	b = min(b, d);
	if(a > b) return 0;
	return b - a + 1;
}

int getans(int x) {
	int L = x, R = ra - la + x;
	return get(L, R, lb, rb) + get(L, R, lb + tb, rb + tb);
}

int solve(int x) {
	if(x < 0) return 0;
	return max(getans((x - la) / g * g + la), getans((x - la + g) / g * g + la));
}

int main() {
	cin >> la >> ra >> ta;
	cin >> lb >> rb >> tb;
	if((ra - la) > (rb - lb)) {
		swap(la, lb);
		swap(ra, rb);
		swap(ta, tb);
	}
	g = gcd(ta, tb);
	// [lb, rb], [lb + tb, rb + tb]
	int ans = 0;
	ans = max(ans, solve(lb));
	ans = max(ans, solve(rb));
	ans = max(ans, solve(lb + tb));
	ans = max(ans, solve(rb + tb));
	ans = max(ans, solve(lb - ta));
	ans = max(ans, solve(rb - ta));
	ans = max(ans, solve(lb + tb - ta));
	ans = max(ans, solve(rb + tb - ta));
	cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL MOD = 998244353;
const int MAXN = 200000 + 5;
const int MAXM = 200000 + 5;

LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool fun(LL a, LL b, LL p, LL q) {
    if(p > q) {
        return false;
    }
    if(p <= 0 && q >= 0) {
        return true;
    }

    LL g = gcd(a, b);
    if(q < 0) {
        p = -p;
        q = -q;
    }
    if(p % g == 0 || q % g == 0) {
        return true;
    }
    if(p / g != q / g) {
        return true;
    }
    return false;
}

int main()
{
    LL la, ra, ta;
    LL lb, rb, tb;
    while(~scanf("%lld%lld%lld", &la, &ra, &ta)) {
        scanf("%lld%lld%lld", &lb, &rb, &tb);
        if(ra - la > rb - lb) {
            swap(la, lb);
            swap(ra, rb);
            swap(ta, tb);
        }
        LL l = 0, r = ra - la + 1;
        while(l <= r) {
            LL m = (l + r) >> 1;
            LL g = gcd(ta, tb);
            bool ok = 0;
            if(fun(ta, tb, lb - la, rb - la + 1 - m)) {
                ok = 1;
            }
            if(fun(ta, tb, lb - ra + m - 1, rb - ra)) {
                ok = 1;
            }
            if(ok) {
                l = m + 1;
            }
            else {
                r = m - 1;
            }
        }
        LL ans = l - 1;
        //LL ans = max(0LL, l - 1);
        printf("%lld\n", ans);
    }
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;
long long la,ra,ta,lb,rb,tb;
long long gcd(long long a, long long b){
	while(b != 0){
		long long r = a%b;
		a = b;
		b = r;
	}
	return a;
}
int main(){
	cin>>la>>ra>>ta>>lb>>rb>>tb;
	long long d = gcd(ta,tb);
	long long offset = floor((lb-la)/d)*d;
	long long right = min(rb,ra+offset);
	long long ans = right - la-offset;
	offset+=d;
	right = min(rb,ra+offset);
	ans = max(ans, right - lb);
	cout<<ans<<endl;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>

#define all(c) c.begin(),c.end()
#define ll long long
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define mod 1000000009
#define PI 3.1415926536
#define pll pair<ll, ll>
using namespace std;

typedef unsigned long long ull;

bool is_prime(ll n) {
    if (n <= 1) {
		return false;
	}
    for (ll i = 2; i * i <= n; ++i) {
            if (n % i == 0) {
                  return false;
            }
    }
    return true;
}

inline ll getPow(ll a,ll b){
    ll res = 1ll,tp = a;
    while (b){
       if (b&1ll) {
           res *= tp;
       }
       tp *= tp;
       b >>= 1ll;
    }
    return res;
}

ll coso(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) {
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

void ok() {
    puts("YES");
    exit(0);
}

void no() {
    puts("NO");
    exit(0);
}

// please, accepted!

inline ll randoming() {
	int tmpp = rand() % 1000000000 * 1000000000 + rand() % 1000000000;
	return tmpp;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll l1 = nxt(), r1 = nxt(), t1 = nxt();
    ll l2 = nxt(), r2 = nxt(), t2 = nxt();
    ll g = __gcd(t1, t2);
    l1 %= g;
    l2 %= g;
    auto get = [&](ll l_1, ll r_1, ll l_2, ll r_2) {
           ll L = max(l_1, l_2);
           ll R = min(r_1, r_2);
           return max(0ll, (R - L + 1));
    };
    ll len1 = (r1 - l1);
    ll len2 = (r2 - l2);
    ll ans = 0;
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
               ll L1 = l1 + i * g;
               ll R1 = L1 + len1;
               ll L2 = l2 + j * g;
               ll R2 = L2 + len2;
               ans = max(ans, get(L1, R1, L2, R2));
        }
    }
    cout << ans;
    return 0;
}

/*
 * parasol
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;
ll l1, r1, t1;
ll l2, r2, t2;

ll gcd(ll a, ll b){
    return b==0?a:gcd(b, a%b);
}

void solve() {
    ll len1 = r1-l1+1;
    ll len2 = r2-l2+1;

    if (gcd(t1, t2) == 1) {
        printf("%lld\n", min(len1, len2));
        return;
    }
    if (len1 > len2) {
        swap(l1, l2);
        swap(r1, r2);
        swap(t1, t2);
    }
    if (l1 > l2) {
            for(ll i = 1; i <= 10020; i++){
                l2 += 100000*t2;
                if (l2 >= l1) break;
            }
        }
        ll ans = 0;
        ll p1 = (l2-l1)%t1;
        if (p1 <= len1) {
            ll lenth1 = len1-p1;
            ans = max(ans, min(lenth1, len2));
        }
        ll p2 = (t1 - p1);
        ans = max(ans, min(len2-p2, len1));
        printf("%lld\n",ans);
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;

    solve();
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxm = 100005;
const ll inf = 2e9;
#define ll long long
ll a[maxm];
ll gcd(ll a, ll b)
{
	if (b == 0) return a;
	return gcd(b, a%b);
}
int main()
{
	int n, i, j, k, sum;
	ll len, ans, tmp, d, num1, num2;
	ll la, lb, ra, rb, ta, tb, lena, lenb;
	scanf("%lld%lld%lld%lld%lld%lld", &la, &ra, &ta, &lb, &rb, &tb);
	lena = ra - la + 1, lenb = rb - lb + 1;
	tmp = gcd(ta, tb);
	d = ((lb - la) % tmp + tmp) % tmp;
	num1 = max((ll)1, min(lenb, lena - d));
	d = (tmp - d) % tmp;
	num2 = max((ll)1, min(lena, lenb - d));
	ans = max(num1, num2);
	printf("%lld\n", ans);
	return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
#define repVector(v)  for( auto it = v.begin(); it != v.last(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define OUTFILE freopen("output.out","w",stdout)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll gcd(ll n1, ll n2) {
	if (n2 != 0) return gcd(n2, n1 % n2);
	else return n1;
}

ll join(ll l1, ll r1, ll l2, ll r2) {
	ll l = max(l1, l2);
	ll r = min(r1, r2);
	return r - l + 1;
}

int main() {
	ll la, ra, ta;
	cin >> la >> ra >> ta;

	ll lb, rb, tb;
	cin >> lb >> rb >> tb;

	ll rangea = ra - la;
	ll rangeb = rb - lb;

	if(lb < la) {
		swap(la, lb);
		swap(ra, rb);
		swap(ta, tb);
	}

	ll ans = 0;

	ll g = gcd(ta, tb);
	debug(g);
	ll delta = 100000;
	ll k = (lb - la) / g;

	debug(k);

	FOR(i, k - delta, k + delta) {
		ll l = la + i * g;
		ll r = l + rangea;
		ans = max(ans, join(lb, lb + rangeb, l, r));
	}

	cout << ans;
}