// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

template<typename Monoid, typename InputIterator> typename Monoid::value_type fold(InputIterator first, InputIterator last) {
  Monoid m;
  auto acc = m.id();
  std::for_each(first, last, [&](auto &x) { acc = m(acc, x); });
  return acc;
}

constexpr int gcd(int a, int b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}

constexpr int lcm(const int a, const int b) { return a / gcd(a, b) * b; }

struct lcmMonoid {
  using value_type = int;
  constexpr value_type id() { return 1; }
  constexpr value_type operator()(const value_type &a, const value_type &b) { return lcm(a, b); }
};

main {
  vi v(6);
  while (cin >> v, accumulate(all(v), 0LL)) {
    vi w(3, 1);
    rep(i, 3) {
      for (int m = v[i * 2] % v[i * 2 + 1]; m != 1; w[i]++) m = (m * v[i * 2]) % v[i * 2 + 1];
    }
    cout << fold<lcmMonoid>(all(w)) << endl;
  }
}