#include <stdio.h>
long x, y;
void exgcd(long long a, long long b) {
if (!b) {
x = 1;
y = 0;
return;
}
exgcd(b, a % b);
long long t = x;
x = y;
y = t - a / b * y;
}
int main() {
long long a, b;
scanf("%lld%lld", &a, &b);
exgcd(a, b);
printf("%lld", (x % b + b) % b);
return 0;
}