#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); string s, t; cin >> s >> t; if(t == "0"){ cout << 1 << endl; return 0; } int ns = s.length(), nt = t.length(); int N = 0; for(int i = 0; i < ns; ++i){ N = (N * 10 + (s[i] - '0')) % 10; } if(N == 0 || N == 1 || N == 5 || N == 6){ cout << N << endl; } else if(N == 4){ cout << (t == "1" ? 4 : 6) << endl; } else{ vector nums(1, N); for(int i = 1; i < 4; ++i){ nums.emplace_back(nums.back() * N % 10); } int M = 0; for(int i = 0; i < nt; ++i){ M = (M * 10 + (t[i] - '0')) % 4; } cout << nums[(M - 1) % 4] << endl; } return 0; }