p,q,r,k = map(int,input().split()) dp = [0]*1005 dp[1] = p%10 dp[2] = q%10 dp[3] = r%10 s = dp[1]*100+dp[2]*10+dp[3] c = 0 for i in range(4,1005): dp[i] = dp[i-1]+dp[i-2]+dp[i-3] dp[i] %= 10 if dp[i-2]*100+dp[i-1]*10+dp[i]==s: c = i-3 break print(dp[k%c] if k%c!=0 else dp[c])