#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long MOD = 1000000007; int main() { string N,M; cin >> N >> M; if ( M == "0" ) { cout << 1 << endl; return 0; } int a = N[N.length()-1] - '0'; int b[4]; int n = a; for ( int i = 0; i < 4; i++ ) { b[i] = n; n *= a; n %= 10; } string s; for ( int i = M.length()-1; i >= 0; i-- ) { s += M[i]; if ( s.length() == 2 ) { break; } } if ( s.length() == 2 ) { swap( s[0], s[1] ); }; int m = stoi(s); cout << b[m%4 + 4 - 1] << endl; return 0; }