# author:  Taichicchi
# created: 09.07.2021 22:48:56

import sys


p, q, r, K = map(int, input().split())

p %= 10
q %= 10
r %= 10

ls = [p, q, r]
s = (p + q + r) % 10
for i in range(3000):
    if K - 1 == i:
        print(ls[i] % 10)
        exit()
    ls.append((ls[-1] + ls[-2] + ls[-3]) % 10)
    if ls[-3] == p and ls[-2] == q and ls[-1] == r and K >= 3000:
        break


print(ls[(K - 1) % (i + 1)])