def get_answer(n, m): if m == 1: return 1 else: if n == 0: return 0 if n == 1: return 1 if n == 2: if m % 4 == 1: return 2 elif m % 4 == 2: return 4 elif m % 4 == 3: return 8 else: return 6 if n == 3: if m % 4 == 1: return 3 elif m % 4 == 2: return 9 elif m % 4 == 3: return 7 else: return 1 if n == 4: if m % 2 == 1: return 4 else: return 6 if n == 5: return 5 if n == 6: return 6 if n == 7: if m % 4 == 1: return 7 elif m % 4 == 2: return 9 elif m % 4 == 3: return 3 else: return 1 if n == 8: if m % 4 == 1: return 8 elif m % 4 == 2: return 4 elif m % 4 == 3: return 2 else: return 6 if n == 9: if m % 2 == 1: return 9 else: return 1 def main(): n = input() m = int(input()) num = int(n[-1]) ans = get_answer(num, m) print(ans) if __name__ == '__main__': main()