from collections import defaultdict p, q, r, k = map(int, input().split()) s = (int(str(p)[-1]), int(str(q)[-1]), int(str(r)[-1])) s_dic = defaultdict(int) s_dic[s] = 1 loop_list = [s] flg = 0 for i in range(k - 3): s = (s[1], s[2], int(str(sum(s))[-1])) if s_dic[s]: flg = 1 break else: s_dic[s] = 1 loop_list.append(s) if flg: k -= 2 k %= len(loop_list) print(loop_list[k - 1][-1]) else: print(s[-1])