#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() template inline bool chmax(A &a, B b) { if (a inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef unsigned long long ull; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair P; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-10; string decr(string a) { if (a == "0") return ""; reverse(ALL(a)); REP(i, a.size()) { if (a[i] != '0') { a[i]--; break; } a[i] = '9'; } if (a.size() > 1 && *(a.end() - 1) == '0') a.erase(a.end() - 1); reverse(ALL(a)); return a; } int main() { string n, m; cin >> n >> m; int ans = -1; if (m == "0") ans = 1; else { int nlast = n[n.size() - 1] - '0'; m = decr(m); vector loop; loop.push_back(nlast); while (loop[0] != loop.back() * nlast % 10) loop.push_back(loop.back() * nlast % 10); int cal = 0; REP(i, m.size()) { cal = cal * 10 + m[i] - '0'; cal %= loop.size(); } ans = loop[cal]; } cout << ans << endl; return 0; }