if __name__ == "__main__": n = int(input()) % 10 m = int(input()) if m == 0: print(n ** m) elif n == 1 or n == 5 or n == 6: print(n) else: d = {2 : [2, 4, 8, 6], 3 : [3, 9, 7, 1], 4 : [4, 6], 7 : [7, 9, 3, 1], 8 : [8, 4, 2, 6], 9 : [9, 1] } no = d[n] print(no[(m - 1) % len(no)])