#include #define be(v) (v).begin(),(v).end() #define pb(q) push_back(q) #define rep(i, n) for(int i=0;i> p >> q; ll g1 = __gcd(p - 1, q - 1); ll g2 = __gcd(p + 1, q + 1); if(g2 == 1) { g2 = __gcd(p, q - 1); cout << (p - 1) / g1 + (q - 1) / g1 + p / g2 + (q - 1) / g2 << endl; return 0; } cout << (p - 1) / g1 + (q - 1) / g1 + (p + 1) / g2 + (q + 1) / g2 << endl; return 0; }