modlist = [[0], [1], [6, 2, 4, 8], [1, 3, 9, 7], [6, 4], [5], [6], [1, 7, 9, 3], [6, 8, 4, 2], [1, 9]] N = raw_input() M = raw_input() newn = 0 newm = 0 if len(M) < 3: newm = int(M) else: newm = int(M[-3]+M[-2]+M[-1]) newn = int(N[-1]) if newm == 0 and len(M) != 1: print 1 else: print modlist[newn][newm%len(modlist[newn])]