扩展欧几里得
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cmath>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
return a;
else
{
gcd(b,a%b);
}
}
void extgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x=1;
y=0;
}
else
{
extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
}
long long x,y,m,n,l;
int main()
{
while(cin>>x>>y>>m>>n>>l)
{
if((x-y)%gcd(n-m,l)!=0)
{
cout<<"Impossible\n"<<endl;
}
else
{
long long p,q;
extgcd((n-m)/gcd(n-m,l),l/gcd(n-m,l),p,q);
p=p*((x-y)/gcd(n-m,l));
p=p%l;
while(p<0)
{
p+=l;
}
cout<<p<<endl;
}
}
return 0;
}