n = int(input()) k = int(input()) n %= 6 k %= 5 if k == 0: k += 5 ans = "285714" print(ans[(n**k + 5) % 6])