#include <bits/stdc++.h>
using namespace std;
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...);
}
long long lA, rA, tA, lB, rB, tB;
long long gcd(long long a, long long 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);
  }
  long long lenA = rA - lA + 1;
  long long lenB = rB - lB + 1;
  long long 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;
  }
  long long dist = lA - lB;
  long long realDist = max(0LL, lenB - dist);
  long long res = min(realDist, lenA);
  lA -= g;
  dist = lB - lA;
  realDist = max(0LL, lenA - dist);
  res = max(res, min(realDist, lenB));
  cout << res;
  return 0;
}
