a,b,c,k=map(int,input().split()) a%=10 b%=10 c%=10 k-=3 v=[[[-1]*10 for i in range(10)] for j in range(10)] l=0 r=0 q=[(a,b,c)] v[a][b][c]=0 while 1: a,b,c=q[-1] aa,bb,cc=b,c,(a+b+c)%10 if v[aa][bb][cc]==-1: r+=1 v[aa][bb][cc]+=v[a][b][c]+1 q+=[(aa,bb,cc)] else: l=v[aa][bb][cc] break if k<=r: print(q[k][-1]) else: print(q[(k-l)%(r-l+1)][-1])