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