import sys p,q,r,K = map(int,input().split()) if K < 4: print([p,q,r][K-1]) sys.exit() K -= 4 K %= 124 A = [p%10,q%10,r%10] for i in range(K+1): a = sum(A) % 10 A = A[1:] + [a] print(a)