# -*- coding:utf-8 -*- import sys if __name__ == "__main__": n = raw_input() m = input() n = int(n[::-1][0]) a = n nums = [n] i = 1 if m == 0: print 1 sys.exit() while i <= m: a *= n a = int(str(a)[::-1][0]) if a == n: break nums.append(a) i += 1 temp = m % i print nums[temp-1]