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])