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