s = '285714' n = int(input()) k = int(input()) print(s[pow(n, k, 7)])