#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair pi; typedef pair pl; typedef pair pls; map m; ll mod[1010]; int main() { ll N, M; cin >> N >> M; ll cnt = 0; for (int i = 0; i <= M; i++) { if (m[((ll)pow(N, i))%10])break; m[((ll)pow(N, i))%10] = 1; mod[cnt++] = ((ll)pow(N, i)) % 10; } int ind; if (N % 2 == 0 && M % cnt == 0) ind = (M % cnt) + 1; else if (N % 2 == 0) { cnt++; ind = M % cnt; } cout << mod[M % cnt] << endl; return 0; }