#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; 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; } int main() { ll N, M; cin >> N >> M; ll g = __gcd(N, M); N /= g; M /= g; int x = 0; int y = 0; while (M % 2 == 0) { x++; M /= 2; } while (M % 5 == 0) { y++; M /= 5; } while (N % 10 == 0) { N /= 10; } if (M != 1) { cout << -1 << endl; return 0; } 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; }