modlist = [[], [1], [2, 4, 8, 6], [3, 9, 7, 1], [4, 6], [5], [6], [7, 9, 3, 1], [8, 4, 2, 6], [9, 1]] N = raw_input() M = raw_input() ans = 1 if M != 0: n = int(N[-1]) m = 0 if len(M) > 2: m = int(M[-3]+M[-2]+M[-1])-1 else: m = int(M)-1 ans = modlist[n][m%len(modlist[n])] print ans