n = int(input()) k = int(input()) n = n % 6 k = k % 6 ans = "285714" ind = (n**k - 1)% 6 print(ans[ind])