p, q, r, k = map(int, input().split()) a = [p, q, r] for i in range((k - 3) % 124): a.append((a[-1] + a[-2] + a[-3]) % 10) print(a[-1])