def main(): N = int(input()) M = int(input()) match N % 10: case 0: print(0) case 1: print(1) case 2: match M % 4: case 0: print(2) case 1: print(4) case 2: print(8) case 3: print(6) case 3: match M % 4: case 0: print(3) case 1: print(9) case 2: print(7) case 3: print(1) case 4: match M % 2: case 0: print(4) case 1: print(6) case 5: print(5) case 6: print(6) case 7: match M % 4: case 0: print(7) case 1: print(9) case 2: print(3) case 3: print(1) case 8: match M % 4: case 0: print(8) case 1: print(4) case 2: print(2) case 3: print(6) case 9: match M % 2: case 0: print(9) case 1: print(1) if __name__ == "__main__": main()