#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector VI; typedef vector VVI; #define REP(i, n) for(int(i)=0;(i)<(n);++(i)) #define FOR(i, f, t) for(int(i)=(f);(i)<(t);(++i)) #define RREP(i, n) for(int(i)=(n)-1;(i)>=0;--(i)) int binpow(ull x, ull k, int m){ if(x==0) return 0; if(k==0) return 1; if(k%2==0) return binpow(((x%m)*(x%m))%m, k/2, m); else return ((x%m)*binpow(x, k-1, m)) % m; } int main(){ do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0); string N,M; cin >> N >> M; ull n = 0, m = 0; for(auto &c : N) n = (n * 10 + (c-'0')) % 10; int MOD = 0; if(M == "0"){ cout << 1 << endl; return 0; } if(n == 0){ cout << 0 << endl; return 0; } if(n == 1){ cout << 1 << endl; return 0; } if(n == 2) MOD = 4; if(n == 3) MOD = 4; if(n == 4) MOD = 2; if(n == 5){ cout << 5 << endl; return 0; } if(n == 6){ cout << 6 << endl; return 0; } if(n == 7) MOD = 4; if(n == 8) MOD = 4; if(n == 9) MOD = 2; for(auto &c : M) m = (m * 10 + (c-'0')) % MOD; cerr << "n = " << n << endl; cerr << "m = " << m << endl; cout << binpow(n,m,10) << endl; return 0; }