#include #define rep(i, a) for (int i = 0; i < (a); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) for (int i = (a) - 1; i >= 0; i--) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) using namespace std; typedef long long ll; const ll inf = 1e9; const ll mod = 1e9 + 7; map primefactor(ll n) { map res; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { res[i]++; n /= i; } } if (n != 1) res[n]++; return res; } ll modpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) (res *= a) %= 10; (a *= a) %= 10; b >>= 1; } return res; } ll power(ll n, ll m) { ll res = 1; rep (i, m) res *= n; return res; } int main() { ll N, M; cin >> N >> M; ll g = __gcd(N, M); N /= g; M /= g; auto pf = primefactor(M); auto pf2 = primefactor(N); bool any = false; for (auto p : pf) { any |= p.first != 2 && p.first != 5; } if (any) { cout << -1 << endl; return 0; } int x = pf[2]; int y = pf[5]; N /= power(10, min(pf2[2], pf2[5])); if (x == y) { cout << N % 10 << endl; } else if (x < y) { ll ans = N % 10 * modpow(2, max(x, y) - x) % 10; cout << ans << endl; } else if (x > y) { ll ans = N % 10 * modpow(5, max(x, y) - y) % 10; cout << ans << endl; } return 0; }