#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int A, B; cin >> A >> B; for (int x = 1; ; ++x) { if (x % A == B % x) { cout << x << "\n"; break; } } return 0; }