#include <iostream>
using namespace std;

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

long lcm(long a, long b) {
  return (a*b) / gcd(a,b);
}

int cycle(int a, int b) {
  int cnt = 1;
  int n = a%b;
  while(n != 1) {
    n = (n*a) % b;
    cnt++;
  }
  return cnt;
}

int main() {
  int a[3], m[3];
  while(cin >> a[0] >> m[0], a[0]+m[0]) {
    for(int i = 1; i < 3; i++) {
      cin >> a[i] >> m[i];
    }

    cout << lcm(lcm(cycle(a[0],m[0]),cycle(a[1],m[1])), cycle(a[2],m[2])) << endl;
  }
}