#include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; using namespace std; #define mod 1000003 #define INF 10000000 #define LLINF 2000000000000000000LL #define SIZE 10000 ll n,m; ll gcd(ll a,ll b){ if(a==0) return b; return gcd(b%a,a); } int main(){ ll ans=0; cin >> n >> m; ll G = gcd(n,m); n/=G; m/=G; if(m==1){ while(n%10==0) n/=10; printf("%lld\n",n%10); return 0; } ll M = m; bool M2=false,M5=false; while(M%2==0){ M/=2; M2=true;} while(M%5==0){ M/=5; M5=true;} if(M!=1){ puts("-1"); return 0; } if(m<(1LL<<55)){ while(n%m>0){ n%=m; n*=10; } printf("%lld\n",n/m); return 0; } if((M2 && M5) || m<(1LL<<55)){ m/=10; while(n%m>0){ n%=m; n*=10; } printf("%lld\n",n/m); return 0; } if(M5){ m/=25; while(n%m>0){ ans*=10; ans+=n/m; ans%=1000; n%=m; n*=10; } while(ans%25>0){ ans%=25; ans*=10; } printf("%lld\n",(ans/25)%10); } if(M2){ m/=16; while(n%m>0){ ans*=10; ans+=n/m; ans%=1000; n%=m; n*=10; } while(ans%16>0){ ans%=16; ans*=10; } printf("%lld\n",(ans/16)%10); } return 0; }