n = int(input())%6 k = int(input()) a = [] v = n while v not in a: a.append(v) v *= n v %= 6 k %= len(a) print('285714'[a[k-1]-1])