#include using namespace std; signed main(){ string N, M; cin >> N >> M; int ld = N.back() - '0'; int m = 1; for( int i = ld * ld % 10; i != ld; i = i * ld % 10 ) ++m; int x = 0; for( int i = 0; i < M.size(); ++i ) x = x * 10 + M[ i ] - '0', x %= m; int ans = ld; for( int i = 0; i < x; ++i ) ans = ans * ld % 10; cout << ans << endl; return 0; }