SN = input() SM = input() N = int(SN[-1]) if SM == '0': ans = 1 else: if N==0: ans = 0 elif N==1: ans = 1 elif N==2: x = int(SM[-2:]) ans = [6,2,4,8][x%4] elif N==3: x = int(SM[-2:]) ans = [1,3,9,7][x%4] elif N==4: x = int(SM[-1]) ans = [6,4][x%2] elif N==5: ans = 5 elif N==6: ans = 6 elif N==7: x = int(SM[-2:]) ans = [1,7,9,3][x%4] elif N==8: x = int(SM[-2:]) ans = [6,8,4,2][x%4] elif N==9: x = int(SM[-1]) ans = [1,9][x%2] print(ans)