N,M = input(),input() n = N % 10 li = [] now = n while now not in li: li.append(now) now *= n now %= 10 if M - 1 < 0: print 1 else: print li[(M - 1)% len(li)]