n = int(raw_input()) % 10 m = int(raw_input()) % 10 l = [n] while len(l) < 2 or l[0] != l[len(l)-1]: l.append((n * l[len(l)-1]) % 10) s = len(l)-1 if m == 0: print 1 else: print l[(m % s)-1]