// http://www.infoarena.ro/problema/inversmodular
#include <stdio.h>
#include <stdlib.h>
void euclidExtins(int a, int b, int *d, int *x, int *y)
{
if (b == 0) {
*d = a;
*x = 1;
*y = 0;
} else {
int x0, y0;
euclidExtins(b, a % b, d, &x0, &y0);
*x = y0;
*y = x0 - (a / b) * y0;
}
}
int main()
{
FILE *f, *g;
int A, N;
int d, x, y;
f = freopen("inversmodular.in", "r", stdin);
g = freopen("inversmodular.out", "w", stdout);
scanf("%d %d", &A, &N);
euclidExtins(A, N, &d, &x, &y);
// x poate fi negativ deci adunam N pana obtinem numar pozitiv
while(x < 0)
x += N;
printf("%d", x);
fclose(f);
fclose(g);
return 0;
}