#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; for( int i = 0; i < x; ++i ) ld = ld * ld % 10; cout << ld << endl; return 0; }