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