import sys sys.set_int_max_str_digits(200000) def main(): loop = "285714" N = int(input()) K = int(input()) print(loop[(pow(N, K, 6) - 1) % 6]) if __name__ == "__main__": main()