#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

//a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

ll divide(ll a, ll b, ll m) {
  ll g = gcd(a, m);
  if (b%g != 0) { return -1; }
  ll x, y;
  extgcd(a, m, x, y);
  assert(a*x+m*y == g);
  ll n = x*b/g;
  ll dn = m/g;
  n -= n/dn*dn;
  if (n < 0)
    n += dn;
  return n;
}

ll calc(const vector<ll> &anss, const vector<ll> &mods) {
  if (anss.size() == 1) { return anss[0]; }
  ll g = gcd(mods[0], mods[1]);
  if (anss[0] % g != anss[1] % g) { return -1; }
  ll t = divide(mods[0], (anss[1] - anss[0] + mods[0] * mods[1]) % mods[1], mods[1]);
  assert(t != -1);
  ll d = lcm(mods[0], mods[1]);
  ll x = (anss[0] + t * mods[0]) % d;
  assert(anss[0] == x % mods[0]);
  assert(anss[1] == x % mods[1]);
  if (x == 0) { x += d; }
  return x;
}

ll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {
  vector<ll> a, m;
  if (l1 == 0 && r1 != 0) { return -1; }
  if (l2 == 0 && r2 != 0) { return -1; }

  if (l1 != 0) {
    ll g = gcd(r1, gcd(l1, m1));
    l1 /= g; r1 /= g; m1 /= g;
    ll v = divide(l1, r1, m1);
    if (v == -1) { return -1; }
    a.push_back(v);
    m.push_back(m1);
  }
  if (l2 != 0) {
    ll g = gcd(r2, gcd(l2, m2));
    l2 /= g; r2 /= g; m2 /= g;
    ll v = divide(l2, r2, m2);
    if (v == -1) { return -1; }
    a.push_back(v);
    m.push_back(m2);
  }
  if (a.size() == 0) { return 1; }
  ll x = calc(a, m);
  if (x > 0) { return x; }
  return -1;
}

ll n, m, a, b, c, d;
int main() {
  while (scanf("%lld %lld %lld %lld %lld %lld", &m, &n, &a, &b, &c, &d) > 0) {
    a %= m; c %= m;
    b %= n; d %= n;
    ll e = 0;
    if (a == 0 && b == 0) {
      swap(a, c);
      swap(b, d);
    }
    if (a == 0 && b == 0) {
      puts("0");
      continue;
    } else if (a == 0) {
      e = n / gcd(b, n);
    } else if (b == 0) {
      e = m / gcd(a, m);
    } else {
      e = lcm(m / gcd(a, m), n / gcd(b, n));
    }
    if (c == 0 && d == 0) {
      printf("%lld\n", e - 1);
      continue;
    }

    ll sqnm = sqrt(n * m) + EPS;
    ll f = 1LL << 60;
    if (e <= sqrt(n * m)) {
      REP(g, e) {
        ll v = solve(c, d, g * a % m, g * b % n, m, n);
        if (v == -1) { continue; }
        assert(v > 0);
        f = min(f, v);
      }
    } else {
      f = 1;
      FOREQ(i, 1, sqnm + 5) {
        ll v = solve(a, b, i * c % m, i * d % n, m, n);
        if (v == -1) { continue; }
        f = i;
        break;
      }
    }
    printf("%lld\n", e * f - 1);
  }
}

Measured memory_bytes: 892