#include <iostream>

int f(const int &a, const int &m) {
	auto res = a;
	int count = 1;
	while (res != 1) {
		res = (res * a) % m;
		++count;
	}
	return count;
}
long long int gcd(const long long int &a, const int &b) {
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
long long int lcd(const long long int &a, const int &b) {
	return a * b / gcd(a, b);
}
int main() {
	long long int min = 1;
	for (auto i = 0; i < 3; ++i) {
		int a, m;
		std::cin >> a >> m;
		min = lcd(min, f(a, m));
	}
	std::cout << min << std::endl;
}