#include using namespace std; int main() { // 1. 入力情報取得. string N, M; cin >> N >> M; // 2. N の M乗 (mod10) の値は? // 2-0. 0乗は, 先に処理. if(M == "0"){ cout << "1" << endl; return 0; } // 2-1. N (mod10) を 記録. int nLen = N.size(); int nMod10 = stoi(N.substr(nLen - 1, 1)); // 2-2. M (mod4) を 記録. int mLen = M.size(); int mMod4 = 0; if(mLen > 1){ int first = stoi(M.substr(mLen - 1, 1)); // 1の位. int second = stoi(M.substr(mLen - 2, 1)); // 10の位. first--; if(first < 0) first += 10, second++; // M = 20 -> M = 19 になった場合などを想定. if(second % 2 == 0) mMod4 = first % 4; if(first % 4 == 0 && second % 2 == 1) mMod4 = 2; if(first % 4 == 1 && second % 2 == 1) mMod4 = 3; if(first % 4 == 2 && second % 2 == 1) mMod4 = 0; if(first % 4 == 3 && second % 2 == 1) mMod4 = 1; } if(mLen == 1){ int first = stoi(M.substr(mLen - 1, 1)); // 1の位. first--; if(first < 0) first += 10; mMod4 = first % 4; } // 3. べき乗の動き. // 3-1. 1の位の値で分岐. int pow[10][4] = {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 4, 8, 6}, {3, 9, 7, 1}, {4, 6, 5, 6}, {5, 5, 5, 5}, {6, 6, 6, 6}, {7, 9, 3, 1}, {8, 4, 2, 6}, {9, 1, 9, 1} }; // 3-2. N (mod10) を M乗した場合 の 1の位の値. int ans = 0; if(nMod10 == 0) ans = 0; if(nMod10 == 1) ans = 1; if(nMod10 == 2) ans = pow[2][mMod4]; if(nMod10 == 3) ans = pow[3][mMod4]; if(nMod10 == 4) ans = pow[4][mMod4]; if(nMod10 == 5) ans = 5; if(nMod10 == 6) ans = 6; if(nMod10 == 7) ans = pow[7][mMod4]; if(nMod10 == 8) ans = pow[8][mMod4]; if(nMod10 == 9) ans = pow[9][mMod4]; // 4. 出力. cout << ans << endl; return 0; }