http://icpc.upc.edu.cn/problem.php?cid=1683&pid=4
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
void exgcd(ll a,ll b,ll &x,ll &y)
{
if (b)
{
exgcd(b,a%b,y,x);
y-=x*(a/b);
}
else
{
x = 1;
y = 0;
}
}
int main()
{
ll a,b,x,y;
scanf("%lld%lld",&a,&b);
exgcd(a,b,x,y);
printf("%lld\n",(x%b+b)%b);
}