import sequtils,strutils proc ctoi(c : char): int = return ord(c) - ord('0') proc stringmod(s : string, m : int) : int = var ans = 0 for i in 0..s.high: ans *= 10 ans += s[i].ctoi ans = ans mod m return ans var N = stdin.readline M = stdin.readline t : int m = N[^1].ctoi e = m * m if M == "0": echo 1 else: if m == 0: echo 0 elif m == 1: echo 1 elif m == 2: case stringmod(M, 4) of 0: echo 6 of 1: echo 2 of 2: echo 4 of 3: echo 8 else: discard elif m == 3: case stringmod(M, 4) of 0: echo 1 of 1: echo 3 of 2: echo 9 of 3: echo 7 else: discard elif m == 4: case stringmod(M, 2) of 0: echo 6 of 1: echo 4 else: discard elif m == 5: echo 5 elif m == 6: echo 6 elif m == 7: case stringmod(M, 4) of 0: echo 1 of 1: echo 7 of 2: echo 9 of 3: echo 3 else: discard elif m == 8: case stringmod(M, 4) of 0: echo 6 of 1: echo 8 of 2: echo 4 of 3: echo 2 else: discard elif m == 9: case stringmod(M, 2) of 0: echo 1 of 1: echo 9 else: discard