import collections d = collections.defaultdict(int) p,q,r,K = map(int, input().split()) A = [p%10,q%10,r%10] for i in range(K-3): if d[(A[1],A[2],sum(A)%10)]>0: temp = (K-d[(A[1],A[2],sum(A)%10)])%(i+3-d[(A[1],A[2],sum(A)%10)]) for i in range(temp): A = [A[1],A[2],sum(A)%10] print(A[-1]) exit() d[(A[1],A[2],sum(A)%10)]=i+3 A = [A[1],A[2],sum(A)%10] print(A[-1])