#include #include using namespace std; string N; string M; int sumFigure(int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += M[i] - '0'; } return sum; } int main() { int ans; cin >> N >> M; if (M == "0") { cout << 1 << endl; return 0; } int digit = N[N.size() - 1] - '0'; int sizeM = M.size(); int sum = sumFigure(sizeM); int lastTwoDigit = (M[sizeM - 1] - '0'); if (sizeM > 1) { lastTwoDigit += (M[sizeM - 2] - '0') * 10; } if (digit == 0 || digit == 1 || digit == 5 || digit == 6) ans = digit; else if (digit == 4 || digit == 9) { if (sum % 2 == 0) { if (digit == 4) ans = 6; else ans = 1; } else { ans = digit; } } else if (digit == 2 || digit == 3 || digit == 7 || digit == 8 || digit == 9) { if (lastTwoDigit % 4 == 0) { if (digit == 2) ans = 6; else if (digit == 3) ans = 1; else if (digit == 7) ans = 1; else if (digit == 8) ans = 6; } else if (lastTwoDigit % 4 == 1) { ans = digit; } else if (lastTwoDigit % 4 == 2) { if (digit == 2) ans = 4; else if (digit == 3) ans = 9; else if (digit == 7) ans = 9; else if (digit == 8) ans = 4; } else if (lastTwoDigit % 4 == 3) { if (digit == 2) ans = 8; else if (digit == 3) ans = 7; else if (digit == 7) ans = 3; else if (digit == 8) ans = 2; } } cout << ans << endl; return 0; }