p,q,r,K = map(int,input().split()) p %= 10 q %= 10 r %= 10 A = [0] * 1001 A[1] = p A[2] = q A[3] = r memo = [0] * 1000 memo[p * 100 + q * 10 + r] = 3 i = 4 while True: A[i] = A[i-1] + A[i-2] + A[i-3] A[i] %= 10 u = A[i] * 100 + A[i-1] * 10 + A[i-2] if memo[u]: loop = i - memo[u] start = memo[u] break else: memo[u] = i i += 1 if K <= start:print(A[K]) else: r = (K - start) % loop print(A[start+r])