# -*- coding: utf-8 -*- N = int(input()) M = int(input()) if M == 0: print(1) else: n = N%10 net_m = M%4 if net_m == 0: net_m = 4 ans = n**net_m % 10 print(ans)