p, q = map(int, input().split()) yl = pow(p, -1, q) xl = p * yl // q yr = pow(-p, -1, q) xr = p * yr // q + 1 print(xl + yl + xr + yr)