# -*- coding:utf-8 -*- if __name__ == "__main__": n = raw_input() m = input() n = int(n[::-1][0]) a = n nums = [n] i = 1 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]